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:
1 class Solution { 2 public int racecar(int target) { 3 if(target == 0){ 4 return 0; 5 } 6 7 int step = 0; 8 LinkedList<int []> que = new LinkedList<>(); 9 que.add(new int[]{0, 1}); 10 int curCount = 1; 11 int nextCount = 0; 12 13 HashSet<String> visited = new HashSet<>(); 14 visited.add(0 + "," + 1); 15 16 while(!que.isEmpty()){ 17 int [] cur = que.poll(); 18 curCount--; 19 20 if(cur[0] == target){ 21 return step; 22 } 23 24 int [] next1 = new int[]{cur[0]+cur[1], 2*cur[1]}; 25 int [] next2 = new int[]{cur[0], cur[1] > 0 ? -1 : 1}; 26 if(0<next1[0] && next1[0]<=target*2 && !visited.contains(next1[0] + "," + next1[1])){ 27 que.add(next1); 28 visited.add(next1[0] + "," + next1[1]); 29 nextCount++; 30 } 31 32 if(!visited.contains(next2[0] + "," + next2[1])){ 33 que.add(next2); 34 visited.add(next2[0] + "," + next2[1]); 35 nextCount++; 36 } 37 38 if(curCount == 0){ 39 curCount = nextCount; 40 nextCount = 0; 41 step++; 42 } 43 } 44 45 return -1; 46 } 47 }
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11531879.html
- 亲身经历的痛--database/sql: Stmt的使用以及坑
- Ubuntu上通过nginx部署Django笔记
- Go学习笔记:golang交叉编译
- Python魔术方法-Magic Method
- python类中super()和__init__()的区别
- Python正则表达式:最短匹配
- 转--Go时间格式化和类型互换操作
- Python标准库(1) — itertools模块
- Linux笔记:使用Vim编辑器
- 一步一步学lucene——(第二步:示例篇)
- 类属性的延迟计算
- 一步一步学lucene——(第三步:索引篇)
- 在Python应用中使用MongoDB
- Python检查xpath和csspath表达式是否合法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Python应用实现双指数函数及拟合代码实例
- 浅谈django不使用restframework自定义接口与使用的区别
- 实例解析php的数据类型
- 实现PHP中session存储及删除变量
- php微信公众号开发之秒杀
- php fread函数使用方法总结
- Yii2框架控制器、路由、Url生成操作示例
- Laravel框架实现调用百度翻译API功能示例
- phpstudy2018升级MySQL5.5为5.7教程(图文)
- laravel实现简单用户权限的示例代码
- tp5(thinkPHP5框架)时间查询操作实例分析
- PHP使Laravel为JSON REST API返回自定义错误的问题
- 详解PHP PDO简单教程
- Python实现ElGamal加密算法的示例代码
- PHP实现基于状态的责任链审批模式详解