initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
executable('canJump', 'src/main.cpp', install: true)
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
inline bool canJump(std::vector<int> &nums) {
|
||||
int max_reach = 0;
|
||||
for (int i = 0; i < int(nums.size()); i++) {
|
||||
if (i > max_reach) return false;
|
||||
max_reach = std::max(max_reach, i + nums[i]);
|
||||
if (max_reach >= int(nums.size()) - 1) return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "canJump.hpp"
|
||||
|
||||
int main() {
|
||||
std::vector<int> nums = {2, 3, 1, 1, 4};
|
||||
bool k = canJump(nums);
|
||||
std::cout << k << std::endl;
|
||||
assert(k == true);
|
||||
nums = {3, 2, 1, 0, 4};
|
||||
k = canJump(nums);
|
||||
std::cout << k << std::endl;
|
||||
assert(k == false);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user