818. Race Car

46人阅读 评论(0) 收藏 举报
分类:

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.

为了减少memory使用量,只记录speed为1的

class Solution:
    def racecar(self, target):
        """
        :type target: int
        :rtype: int
        """
        res = 0
        q,qq=[(1,0)],[]
        vs = set()
        vs.add((1,0))
        while q:
            while q:
                ts,tp=q.pop()
                if tp==target: 
                    return res
                if (2*ts,tp+ts) not in vs:
                    qq.append((2*ts,tp+ts))
#                    vs.add((2*ts,tp+ts))    # only start point need to be memoried
                    qq.append((-1 if ts>0 else 1,tp))
                    vs.add((-1 if ts>0 else 1,tp))
            q,qq=qq,q
            res+=1
    
s=Solution()
#print(s.racecar(3))
#print(s.racecar(6))
#print(s.racecar(5))
print(s.racecar(6102))

查看评论

leetcode 818. Race Car

leetcode 818. Race Car 原题地址:https://leetcode.com/problems/race-car/ 题目 Your car starts at pos...
  • birdreamer
  • birdreamer
  • 2018-04-16 11:05:30
  • 45

818. Race Car

Your car starts at position 0 and speed +1 on an infinite number line.  (Your car can go into negati...
  • zjucor
  • zjucor
  • 2018-04-15 15:57:27
  • 46

Servlet 的 URL 匹配规则

路径映射规则 在Web应用程序部署描述符中,共有五种映射规则: 路径匹配:以 / 字符开始并以 /*后缀结尾的字符串用于路径映射。 扩展匹配:以 *. 前缀开始的字符串用作扩展映射 空字符串 ...
  • zsensei
  • zsensei
  • 2018-01-25 21:23:51
  • 48

Sicily 1264. Atomic Car Race[Special judge]

题目链接 soj.me/1264 题目
  • ccchennn
  • ccchennn
  • 2014-10-02 21:49:18
  • 590

leetCode练习(125)

题目:Valid Palindrome 难度:easy 问题描述: Given a string, determine if it is a palindrome, considering on...
  • u010771890
  • u010771890
  • 2016-11-04 13:54:59
  • 370

Use you arrow keys to race around the track in this cool rac

  • 2006年02月23日 09:05
  • 421KB
  • 下载

Linux.Performance.Tunning.and.Capacity.Planning.pdf

  • 2009年05月14日 11:26
  • 4MB
  • 下载

Car race game 树状数组

Car race game Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot...
  • ZQUSwansea
  • ZQUSwansea
  • 2016-03-13 22:29:00
  • 320

Sicily.1264. Atomic Car Race

一道挺简单的DP题。关键在于理解题意。这道题设了不少的语言难度在里面,要理解行驶时间的计算以及最短行驶时间的推导。 首先来看下行驶时间,这个很关键,我刚开始做的时候觉得这道题不知所云就是因为理解不了...
  • ArthurFree
  • ArthurFree
  • 2014-07-06 12:56:55
  • 733

Car race game

题意: 输入一个n以EOF作为结束,之后n行每行输入一个x,一个v,x代表当前车的坐标,v代表车的速度,问最后总共的超车数。   解题思路: 思路其实很简单,用将车排序,按照x1y2排序。然后...
  • baoluqi
  • baoluqi
  • 2015-08-25 09:55:36
  • 168