-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55.jump-game.cpp
48 lines (40 loc) · 1.06 KB
/
55.jump-game.cpp
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
/*
* @lc app=leetcode id=55 lang=cpp
*
* [55] Jump Game
*/
// @lc code=start
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
if (nums.size() == 1 && nums[0] == 0) return true;
int maxValueBuffer = -1;
int maxValueIndexBuffer = -1;
int tempMaxValueBuffer = -1;
for (int i = 0; i < nums.size(); i++) {
if (tempMaxValueBuffer - 1 <= nums[i]) {
maxValueBuffer = nums[i];
maxValueIndexBuffer = i;
tempMaxValueBuffer = maxValueBuffer;
}
else {
tempMaxValueBuffer -= 1;
}
const int minimumValue = i == nums.size() - 1 ? i - maxValueIndexBuffer : i - maxValueIndexBuffer + 1;
if (maxValueBuffer < minimumValue)
{
return false;
}
}
return true;
}
};
// @lc code=end