-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55_jump_game.cpp
More file actions
39 lines (30 loc) · 903 Bytes
/
55_jump_game.cpp
File metadata and controls
39 lines (30 loc) · 903 Bytes
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
#include <vector>
#include <iostream>
#include <sstream>
// O(N)
class Solution {
public:
bool canJump(std::vector<int>& nums) {
if (!nums.size()) return true;
std::size_t maxReach = 0;
for (std::size_t i = 0; i < nums.size(); i++){
if (maxReach >= nums.size() - 1) return true;
if (nums.at(i) == 0 && maxReach <= i) return false;
maxReach = std::max(maxReach, i + static_cast<std::size_t>(nums.at(i)));
}
return true;
}
};
int main(){
std::string line;
std::cout << "Space separated array: ";
std::getline(std::cin, line);
std::vector<int> nums;
int v;
std::istringstream iss(line);
while(iss >> v)
nums.push_back(v);
std::string answer = Solution().canJump(nums) ? "yes" : "no";
std::cout << "Can jump: " << answer << std::endl;
return 0;
}