forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump-game-ii.py
50 lines (47 loc) · 1.5 KB
/
jump-game-ii.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
# Time: O(n)
# Space: O(1)
#
# 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.)
#
class Solution:
# @param A, a list of integers
# @return an integer
def jump(self, A):
jump_count = 0
reachable = 0
curr_reachable = 0
for i, length in enumerate(A):
if i > reachable:
return -1
if i > curr_reachable:
curr_reachable = reachable
jump_count += 1
reachable = max(reachable, i + length)
return jump_count
# Time: O(n^2)
# Space: O(1)
class Solution2:
# @param A, a list of integers
# @return an integer
def jump(self, A):
result, prev_reachable, reachable = 0, -1, 0
while reachable > prev_reachable:
if reachable >= len(A) - 1:
return result
result += 1
prev_reachable = reachable
for i, length in enumerate(A[:reachable + 1]):
reachable = max(reachable, i + length)
return -1
if __name__ == "__main__":
print Solution().jump([2,3,1,1,4])
print Solution().jump([3,2,1,0,4])