initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
executable('jump', 'src/main.cpp', install: true)
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <vector>
|
||||
|
||||
inline int jump(std::vector<int> &nums) {
|
||||
int n = nums.size();
|
||||
if (n <= 1) return 0;
|
||||
int jumps = 0;
|
||||
int current_end = 0;
|
||||
int farthest = 0;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
farthest = std::max(farthest, i + nums[i]);
|
||||
if (i == current_end) {
|
||||
jumps += 1;
|
||||
current_end = farthest;
|
||||
if (current_end >= n - 1) break;
|
||||
}
|
||||
}
|
||||
return jumps;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include "jump.hpp"
|
||||
|
||||
int main() {
|
||||
std::vector<int> nums = {2, 3, 1, 1, 4};
|
||||
int output = jump(nums);
|
||||
assert(output == 2);
|
||||
nums = {2, 3, 0, 1, 4};
|
||||
output = jump(nums);
|
||||
assert(output == 2);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user