forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJump_Game_II.py
More file actions
45 lines (39 loc) · 1.33 KB
/
Jump_Game_II.py
File metadata and controls
45 lines (39 loc) · 1.33 KB
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
"""
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):
N = len(A)
dp = [N for i in range(N)]
dp[0] = 0
for i in range(N):
for j in range(i)[::-1]:
if A[j] + j >= i:
dp[i] = min(dp[i], dp[j]+1)
return dp[N-1]
# Note:
# 1. dp means jump to i, the min jump steps
# 2. dp[0] = 0
# 3. dp[i] = min(dp[i],dp[j]+1) if A[j] + j >= i
def jump(self, A):
n = len(A)
if n == 1:
return 0
res = 0
start = 0
while start < n-1:
res += 1
if start + A[start] >= n-1:
return res
max_step = start
for i in range(start+1, start+A[start]+1):
if i + A[i] >= max_step + A[max_step]: # Here doesn't have to be >=
max_step = i
start = max_step