-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0045.Jump_Game_II.java
More file actions
69 lines (55 loc) Β· 1.74 KB
/
0045.Jump_Game_II.java
File metadata and controls
69 lines (55 loc) Β· 1.74 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Given an array of non-negative integers nums, 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.
You can assume that you can always reach the last index.
Example 1:
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: 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.
Example 2:
Input: nums = [2,3,0,1,4]
Output: 2
*/
"""
Python
1.define state: dp[i] the minimum number of jumps
2.get dp[n-1]
3.dp[0] = 0 dp[1] = 1
4.transit function dp[j] = min{dp[i] + 1} 0 < i < j if i + nums[i] >= j
"""
class Solution:
def jump(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return 0
dp = [float('inf')] * n
dp[0] = 0
dp[1] = 1
for i in range(2, n):
for j in range(i):
if j + nums[j] >= i:
dp[i] = min(dp[i], dp[j] + 1)
return dp[n-1]
/*
1.definition dp[i] represent the minimum number of jumps to reach the i index
2.we require dp[n-1]
3.initialization initialize the dp[0] = 0
4.recursion fomular dp[i] = min(d[j] + 1) each j from 0 to i-1 && A[j] >= i-j
*/
class Solution {
public int jump(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] >= i - j) {
dp[i] = Math.min(dp[i], dp[j] + 1);
}
}
}
return dp[n-1];
}
}