-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJump_Game_2.cpp
More file actions
40 lines (37 loc) · 1.04 KB
/
Jump_Game_2.cpp
File metadata and controls
40 lines (37 loc) · 1.04 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
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size();
int step = 0; // 最小步数
int left = 0;
int right = 0; // [left, right] 是当前能覆盖的区间
if (n == 1) return 0;
while (left <= right)
{ // 尝试从每一层跳最远
++step;
const int old_right = right;
for (int i = left; i <= old_right; ++i)
{
int new_right = i + nums[i];
if (new_right >= n - 1) return step;
if (new_right > right) right = new_right;
}
left = old_right + 1;
}
return 0;
}
//时间复杂度:O(n),空间复杂度:O(1)
int jump(vector<int>& nums) {
int last = 0, step = 0, reach = 0;
for (int i = 0; i <= reach && i < nums.size(); ++i) //i<=reach:考虑元素为0的情况
{
if (i > last) //i超过上一步能够到达的最远位置
{
++step;
last = reach;
}
reach = max(reach, nums[i] + i); //更新能够到达的最远位置
}
return step;
}
};