原题链接在这里:https://leetcode.com/problems/race-car/

题目:

Your car starts at position 0 and speed +1 on an infinite number line.  (Your car can go into negative positions.)

Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse).

When you get an instruction "A", your car does the following: position += speed, speed *= 2.

When you get an instruction "R", your car does the following: if your speed is positive then speed = -1 , otherwise speed = 1.  (Your position stays the same.)

For example, after commands "AAR", your car goes to positions 0->1->3->3, and your speed goes to 1->2->4->-1.

Now for some target position, say the length of the shortest sequence of instructions to get there.

Example 1:
Input:
target = 3
Output: 2
Explanation:
The shortest instruction sequence is "AA".
Your position goes from 0->1->3.
Example 2:
Input:
target = 6
Output: 5
Explanation:
The shortest instruction sequence is "AAARA".
Your position goes from 0->1->3->7->7->6.

Note:

  • 1 <= target <= 10000.

题解:

Use BFS to find out shortest sequence.

Each step, there are 2 possibilities, either A or R. Maintain a set to store visited state.

If current position is alrady beyond 2 * target, it can't make shortest path.

Time Complexity: O(nlogn). n = target. totoally there are 2*n positions, each position could be visited 2*logn times at most. Since the speed could not be beyond logn.

Space: O(n).

AC Java:

 class Solution {
public int racecar(int target) {
if(target == 0){
return 0;
} int step = 0;
LinkedList<int []> que = new LinkedList<>();
que.add(new int[]{0, 1});
int curCount = 1;
int nextCount = 0; HashSet<String> visited = new HashSet<>();
visited.add(0 + "," + 1); while(!que.isEmpty()){
int [] cur = que.poll();
curCount--; if(cur[0] == target){
return step;
} int [] next1 = new int[]{cur[0]+cur[1], 2*cur[1]};
int [] next2 = new int[]{cur[0], cur[1] > 0 ? -1 : 1};
if(0<next1[0] && next1[0]<=target*2 && !visited.contains(next1[0] + "," + next1[1])){
que.add(next1);
visited.add(next1[0] + "," + next1[1]);
nextCount++;
} if(!visited.contains(next2[0] + "," + next2[1])){
que.add(next2);
visited.add(next2[0] + "," + next2[1]);
nextCount++;
} if(curCount == 0){
curCount = nextCount;
nextCount = 0;
step++;
}
} return -1;
}
}

LeetCode 818. Race Car的更多相关文章

  1. 【leetcode最短路】818. Race Car

    https://leetcode.com/problems/race-car/description/ 1. BFS剪枝 0<=current position<=2*target.为什么 ...

  2. 818. Race Car

    Your car starts at position 0 and speed +1 on an infinite number line.  (Your car can go into negati ...

  3. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  4. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  5. [LeetCode] Race Car 赛车

    Your car starts at position 0 and speed +1 on an infinite number line.  (Your car can go into negati ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  8. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  9. 刷LeetCode的正确姿势——第1、125题

    最近刷LeetCode比较频繁,就购买了官方的参考电子书 (CleanCodeHandbook),里面有题目的解析和范例源代码,可以省去非常多寻找免费经验分享内容和整理这些资料的时间.惊喜的是,里面的 ...

随机推荐

  1. Java多线程编程(3)--线程安全性

    一.线程安全性   一般而言,如果一个类在单线程环境下能够运作正常,并且在多线程环境下,在其使用方不必为其做任何改变的情况下也能运作正常,那么我们就称其是线程安全的.反之,如果一个类在单线程环境下运作 ...

  2. Python之路【第二十篇】:python项目之旧版抽屉新热榜

    旧版抽屉新热榜 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  3. 2. 运行Spark Streaming

    2.1 IDEA编写程序 Pom.xml加入以下依赖: <dependency> <groupId>org.apache.spark</groupId> <a ...

  4. jedis异常:Could not get a resource from the pool

    前几天公司后端系统出现了故障,导致app多个功能无法使用,查看日志,发现日志出现较多的redis.clients.jedis.exceptions.JedisConnectionException: ...

  5. Java异常体系结构学习笔记

    异常类的继承层次       1.Throwable是所有异常类的父类,他也继承自Object.所以Throwable是一个类,而不是接口. 2.Error这个分支的异常是由于Java虚拟机内部错误导 ...

  6. kubernetes使用securityContext和sysctl

    前言 在运行一个容器时,有时候需要使用sysctl修改内核参数,比如net..vm..kernel等,sysctl需要容器拥有超级权限,容器启动时加上--privileged参数即可.那么,在kube ...

  7. 用StatSVN统计svn项目中每人代码提交量

    用StatSVN统计SVN服务器项目的代码量 下载并安装SVN统计工具StatSVN 1)下载地址:sourceforge.net/projects/statsvn/ 2)解压压缩包,到一个目录,如D ...

  8. 使用 Create-React-App 开发 Chrome 扩展

    整理 Kindle 标注.书签和笔记从未如此简单! Kindle 标注管理应用 Kindle Mate 只支持 Windows,不支持 Mac.标注只是解析我的剪贴文本文件,配合 FileReader ...

  9. ansible 中 JAVA_HOME不生效问题

    解决方案 ~/.bash_profile 是交互式.login 方式进入 bash 运行的,意思是只有用户登录时才会生效. ~/.bashrc 是交互式 non-login 方式进入 bash 运行的 ...

  10. SQL常见的一些面试题(太有用啦)

    SQL常见面试题 1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name   kecheng   fenshu张三    语文       81张三     数学       75李四 ...