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.

Approach #1: C++. [DFS]

class Solution {
public:
int racecar(int target) {
queue<pair<int, int>> q;
q.push({0, 1});
unordered_set<string> v;
v.insert("0_1");
v.insert("0_-1");
int steps = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
auto p = q.front(); q.pop();
int pos = p.first;
int speed = p.second;
{
int pos1 = pos + speed;
int speed1 = speed * 2;
pair<int, int> p1{pos1, speed1};
if (pos1 == target) return steps+1;
if (p1.first > 0 && p1.first < 2 * target)
q.push(p1);
}
{
int speed2 = speed > 0 ? -1 : 1;
pair<int, int> p2{pos, speed2};
string key2 = to_string(pos) + "_" + to_string(speed2);
if (!v.count(key2)) {
q.push(p2);
v.insert(key2);
}
}
}
steps++;
}
return -1;
}
};

  

Approach #2: Java. [DP]

class Solution {
private static int[][] m;
public int racecar(int target) {
if (m == null) {
final int kMaxT = 10000;
m = new int[kMaxT + 1][2];
for (int t = 1; t <= kMaxT; ++t) {
int n = (int)Math.ceil(Math.log(t + 1) / Math.log(2));
if (1 << n == t + 1) {
m[t][0] = n;
m[t][1] = n + 1;
continue;
}
int l = (1 << n) - 1 - t;
m[t][0] = n + 1 + Math.min(m[l][1], m[l][0] + 1);
m[t][1] = n + 1 + Math.min(m[l][0], m[l][1] + 1);
for (int i = 1; i < t; ++i) {
for (int d = 0; d <= 1; ++d) {
m[t][d] = Math.min(m[t][d], Math.min(
m[i][0] + 2 + m[t-i][d],
m[i][1] + 1 + m[t-i][d]));
}
}
}
}
return Math.min(m[target][0], m[target][1]);
}
}

  

Approach #3: Python. [DP]

class Solution(object):
def __init__(self): self.dp = {0: 0} def racecar(self, t):
"""
:type target: int
:rtype: int
"""
if t in self.dp: return self.dp[t]
n = t.bit_length()
if 2**n - 1 == t: self.dp[t] = n
else:
self.dp[t] = self.racecar(2**n - 1 - t) + n + 1
for m in range(n-1):
self.dp[t] = min(self.dp[t], self.racecar(t - 2**(n-1) + 2**m) + n + m + 1)
return self.dp[t]

  

Analysis:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-818-race-car/

818. Race Car的更多相关文章

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

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

  2. LeetCode 818. Race Car

    原题链接在这里:https://leetcode.com/problems/race-car/ 题目: Your car starts at position 0 and speed +1 on an ...

  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. Promise.race

    [Promise.race] 返回最先完成的promise var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 5 ...

  6. golang中的race检测

    golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...

  7. 【BZOJ-2599】Race 点分治

    2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBSubmit: 2590  Solved: 769[Submit][Status ...

  8. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  9. Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...

随机推荐

  1. 第二届PHP全球开发者大会(含大会的PPT)

    PHP全球开发者大会于2016年5月14日至15日在北京召开 更多现场图片请猛击: http://t.cn/RqeP7y9 ,  http://t.cn/RqD8Typ 最后,这次大会的PPT可以在这 ...

  2. JSF页面中的JS取得受管bean的数据(受管bean发送数据到页面)

    JSF中引入jsf.js文件之后,可以拦截jsf.ajax.request请求.一直希望有一种方法可以像jquery的ajax一样,能在js中异步取得服务器端发送的数据.无奈标准JSF并没有提供这样的 ...

  3. python第二十三天-----Tornado

    Tornado是一个轻量级完整的web框架,在Linux系统下它会使用epoll,是一个异步非阻塞的web服务器框架,对于实时应用来说很理想,想想同是异步非阻塞的nginx的残暴程度就知道了 1.路由 ...

  4. Codeforces Round #310 (Div. 2)556ABCDE

    https://github.com/Anoxxx/OI/blob/master/Anoxx/Contest10 github自取

  5. 监控和安全运维 1.4 nagios安装

    1. Nagios 简介是一个开源软件,可以监控网络设备网络流量.Linux/windows主机状态,甚至可以监控打印机它可以运行在Linux上或windows上基于浏览器的web界面方便运维人员查看 ...

  6. ListView显示Sqlite的数据

    在安卓中,ListView和Sqlite都是十分常用的.这次我们来结合这个两个知识点写一个Demo. 功能:吧SQLite中的数据用ListView显示出来. 先看截图吧 首先是数据库 然后是运行截图 ...

  7. IntelliJ IDEA 导入Project

    一.方式一 File---->Close Project 这样的户每次需要import,都要close一次,非常不方便,如果能在File下面岂不是更好? 二.方式二 File---->Se ...

  8. ie6-ie8不支持opacity,rgba解决方法

    半透明部分设置样式:opacity:0.7在ie9/ie10/ff/chrome/opera/safari显示正常. 但是这样在ie6-ie8中是不支持的,需要加上下面这句话: filter: pro ...

  9. Eclipse中,将jar包导入为User Library

    项目右键 Properties -> bulid path -> Add Library -> User Library -> User Libraries -> New ...

  10. 激活 jave platform se

    1.有的网页上面会显示这个,但是点击后没反应 解决方案: firefox- >工具-附加组件管理器-“插件” 找到“Java(TM) Platform SE 7”,把“询问是否激活”改为“总是激 ...