原题链接在这里: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. 解决 niceScroll 自适应DOM 高度变化

    利用dataTable展示数据列表时,当选择每页显示数量时,滚动条还是按照页面初始化时显示的,导致无法滚动查看下面的数据,  在stackoverflower 找到一个可用的方法,但不知道为什么仅写  ...

  2. mysql 复制表结构(包括索引等)、表内容

    =============================================== 2019/7/16_第1次修改                       ccb_warlock == ...

  3. aria2 adduri

    demo, ok import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handl ...

  4. springCloud学习笔记2(服务发现)

    本篇代码存放于:https://github.com/FleyX/demo-project/tree/master/springcloud/spring-cloud%E6%9C%8D%E5%8A%A1 ...

  5. Mac 指令

    Mac 展示隐藏目录 // 设置隐藏文件不可见 defaults write com.apple.finder AppleShowAllFiles FALSE // 设置隐藏文件可见 defaults ...

  6. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  7. Hive Lateral View

    一.简介 1.Lateral View 用于和UDTF函数[explode,split]结合来使用. 2.首先通过UDTF函数将数据拆分成多行,再将多行结果组合成一个支持别名的虚拟表. 3.主要解决在 ...

  8. java程序员常用的cmd命令

    1.查看端口号或者进程号使用情况 1.1.查看所有端口占用情况 C:\Users\Administrator>netstat -ano 活动连接 协议 本地地址 (ip:端口) 外部地址 状态 ...

  9. Android开发之常用Intent.Action【转】

    1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...

  10. glfw之hello world

    mac上用cocoa做imshow,资料似乎不好找,即便找到也需要和OC混编,而不是纯C.这不够纯粹.考虑用opengl做通用的.跨平台的imshow.先入门一下opengl,从glfw官方例子入手. ...