LeetCode 818. Race Car
原题链接在这里: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的更多相关文章
- 【leetcode最短路】818. Race Car
https://leetcode.com/problems/race-car/description/ 1. BFS剪枝 0<=current position<=2*target.为什么 ...
- 818. Race Car
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negati ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- [LeetCode] Race Car 赛车
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negati ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 刷LeetCode的正确姿势——第1、125题
最近刷LeetCode比较频繁,就购买了官方的参考电子书 (CleanCodeHandbook),里面有题目的解析和范例源代码,可以省去非常多寻找免费经验分享内容和整理这些资料的时间.惊喜的是,里面的 ...
随机推荐
- CORS解决跨域问题(403问题)
1.什么是跨域问题? 跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是用当前页同域名同端口的路径,这能有效的阻止跨站攻击. 2.跨域问题出现的条件: 1.跨域问题是a ...
- Python之路【第二十二篇】:轮播图片CSS
轮播代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- C语言合并果子-贪心算法
/*有几堆水果.每次你把两堆东西移到一起,形成更大的一堆.每个动作消耗的能量是两堆水果的总重量.如何把所有的水果堆在一起,消耗最少的能量?*/ 以上是题目,该题首先要读懂题目,每次移到一起以后都要将数 ...
- vs2017- C语言- winsocket- 链接错误 LNK2019
错误介绍 操作系统:windows10 IDE:vs2017 语言:C语言 项目内容简介:编写一个双人网络海战棋对战游戏 错误类型:链接错误 LNK2019 解决方案:程序需要用到ws2_32.lib ...
- PyCharm+QTDesigner学习
PyCharm+QTDesigner+PyUIC使用教程:https://www.cnblogs.com/lsdb/p/9122425.html python用pyinstaller生成exe时报错 ...
- 对一次 redis 未授权写入攻击的分析以及学习
前段时间自己使用 redis 开发的时候,搞了一个 docker ,然后直接开放连接没有密码,其实一开始我就知道会被黑产扫到然后给我种马,但是把因为也是测试服务,其实也没怎么上心,于是就放任自由了,结 ...
- Java自学-数组 复制数组
Java 如何复制数组 数组的长度是不可变的,一旦分配好空间,是多长,就多长,不能增加也不能减少 步骤 1 : 复制数组 把一个数组的值,复制到另一个数组中 System.arraycopy(src, ...
- 一个简单实现的string类
为了复习c++知识,简单的实现一个string类,类名为CMyString 环境说明:windows 7 64位 和 CentOS Linux release 7.6.1810 (Core) 开发工具 ...
- git恢复已删的分支
git恢复已经删除的分支 执行git命令, 找回之前提交的commit git log -g 执行效果 commit 80fd3a3e1abeab52030ee9f6ec32b5c815de20a9 ...
- 【开发工具】-Idea代码提示忽略大小写
设置路径:File–>Settings–>Editor–>General–>Code Completion–>Match case 取消Match case 勾选. [o ...