-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJumpGame.java
More file actions
163 lines (152 loc) · 4.42 KB
/
Copy pathJumpGame.java
File metadata and controls
163 lines (152 loc) · 4.42 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* <p>给定一个非负整数数组 <code>nums</code> ,你最初位于数组的 <strong>第一个下标</strong> 。</p>
*
* <p>数组中的每个元素代表你在该位置可以跳跃的最大长度。</p>
*
* <p>判断你是否能够到达最后一个下标。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums = [2,3,1,1,4]
* <strong>输出:</strong>true
* <strong>解释:</strong>可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums = [3,2,1,0,4]
* <strong>输出:</strong>false
* <strong>解释:</strong>无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
* <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
* </ul>
* <div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 1536</li><li>👎 0</li></div>
*/
package leetcode4;
public class JumpGame {
public static void main(String[] args) {
Solution solution = new JumpGame().new Solution();
}
/**
* 双指针,挨个跳
*/
class Solution0 {
public boolean canJump(int[] nums) {
int right = 0;
for (int left = 0; left <= right && left < nums.length; left++) {
right = Math.max(right, nums[left] + left);
}
return right >= nums.length - 1;
}
}
/**
* DP完整版:DP[n] = Max( DP[n - 1], arr[n] + n )
*/
class Solution {
public boolean canJump(int[] nums) {
if (nums.length < 0) {
return true;
}
int[] dp = new int[nums.length];
dp[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
if (dp[i - 1] < i) {
return false;
}
dp[i] = Math.max(dp[i - 1], i + nums[i]);
}
return true;
}
}
/**
* DP优化空间复杂度
*/
class Solution1 {
public boolean canJump(int[] nums) {
int prev = nums[0];
for (int i = 1; i < nums.length; i++) {
if (prev < i) {
return false;
}
prev = Math.max(prev, i + nums[i]);
}
return true;
}
}
// ------ 后面的解法都不研究了 ------
/**
* 倒序
*/
class Solution2 {
public boolean canJump(int[] nums) {
if (nums.length <= 1) {
return true;
}
int min = nums.length - 1;
for (int i = nums.length - 2; i >= 0; i--) {
if (i + nums[i] >= min) {
min = i;
}
}
return min == 0;
}
}
/**
* DFS,耗时较高
*/
class Solutio3 {
public boolean canJump(int[] nums) {
boolean[] memo = new boolean[nums.length];
return resolve(0, nums, memo);
}
private boolean resolve(int i, int[] nums, boolean[] memo) {
if (i >= nums.length - 1) {
return true;
}
if (memo[i]) {
return false;
}
for (int num = nums[i]; num > 0; num--) {
int j = i + num;
if (resolve(j, nums, memo)) {
return true;
} else {
memo[j] = true;
}
}
return false;
}
}
/**
* 遍历,耗时较高
*/
class Solution4 {
public boolean canJump(int[] nums) {
if (nums.length <= 1) {
return true;
}
boolean[] memo = new boolean[nums.length];
memo[0] = true;
for (int i = 0; i < nums.length - 1; i++) {
if (!memo[i]) {
continue;
}
for (int j = 1; j <= nums[i]; j++) {
memo[Math.min(nums.length - 1, i + j)] = true;
}
}
return memo[nums.length - 1];
}
}
}