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),里面有题目的解析和范例源代码,可以省去非常多寻找免费经验分享内容和整理这些资料的时间.惊喜的是,里面的 ...
随机推荐
- Linux基础(05)socket编程
Linux的核心思想之一 "一切皆文件" 内容 , socket在Linux内核的实现的代码及TCP和UDP的实现 网络编程常用头文件: https://blog.csdn.net ...
- Docker容器挂载文件(转载)
一.Docker pull 安装 Nginx 1.查看docker仓库中的 nginx 命令 # 使用 docker search 命令搜索存放在 Docker Hub 中的镜像 docker sea ...
- appium_获取元素状态
元素的属性我们经常会用到,当定位到某个元素后,有时会需要用到这个元素的text值.className.resource-id.checked等. 一般标准的属性我们都可以通过get_attribut ...
- Golang 常用的第三方包.
Goland 下面这个license server 可用 http://idea.youbbs.org (2018-01-10 04:26:09) http://45.77.127.87:81(201 ...
- MVC视图中 TextBoxFor 数据格式化
@Html.TextBoxFor(m => m.Birthday,"{0:yyyy-MM-dd}", new { @class = "m-wrap small&qu ...
- 微软企业库支持 MySql
微软企业库支持 MySql 三步让企业库支持 mysql 数据库 1.创建 MySqlDatabaseData 类 using Microsoft.Practices.EnterpriseLibr ...
- CSS-宽度自适应和浏览器兼容笔记
自适应 宽度自适应:网页元素根据窗口或子元素自动调整宽度 适用百分比进行设置,例如:100% 铺满:50% 占据一般宽度 块元素如果不设置宽度,默认为100% 自适应中可以设置最大或者最小宽度和高度 ...
- Telnet入侵Windows2000
开启Telnet 打开控制面板,管理工具 计算机管理 连接刚刚探测到的主机 输入探测到的主机IP 如下图所示,连接成功 找到Telnet服务 启动Telnet服务 远程登录 注意 Telnet登录需要 ...
- 【书评:Oracle查询优化改写】第二章
[书评:Oracle查询优化改写]第二章 BLOG文档结构图 在上一篇中http://blog.itpub.net/26736162/viewspace-1652985/,我们主要分析了一些单表查询的 ...
- day 20 作业
作业 1.下面这段代码的输出结果将是什么?请解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent ...