-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path45_jump_game_2.py
57 lines (48 loc) · 1.58 KB
/
45_jump_game_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
Note:
You can assume that you can always reach the last index.
'''
class Solution(object):
def jump_too_slow(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
stepsReq = [999999999 for _ in xrange(len(nums))]
stepsReq[0] = 0
for i in xrange(len(nums)):
n = nums[i]
for j in xrange(1, n+1):
if i+j >= len(stepsReq):
break
stepsReq[i+j] = min(stepsReq[i] + 1, stepsReq[i+j])
return stepsReq[-1]
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
stepsReq = [-1 for _ in xrange(len(nums))]
stepsReq[0] = 0
i = 0
curSteps = 0
furthest = nums[0]
while stepsReq[-1] == -1:
nextFurthest = 0
while i <= furthest and i < len(nums):
stepsReq[i] = curSteps + 1
nextFurthest = max(nextFurthest, nums[i] + i)
i += 1
curSteps += 1
furthest = nextFurthest
return stepsReq[-1]
s = Solution()
print s.jump([2,3,1,1,4])
print s.jump([1, 2, 3])
print s.jump([0])