MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / jumpToTheEnd

Function jumpToTheEnd

cpp/Greedy/jump_to_the_end.cpp:3–18  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include <vector>
2
3bool jumpToTheEnd(std::vector<int>& nums) {
4 // Set the initial destination to the last index in the array.
5 int destination = nums.size() - 1;
6 // Traverse the array in reverse to see if the destination can be
7 // reached by earlier indexes.
8 for (int i = nums.size() - 1; i >= 0; i--) {
9 // If we can reach the destination from the current index,
10 // set this index as the new destination.
11 if (i + nums[i] >= destination) {
12 destination = i;
13 }
14 }
15 // If the destination is index 0, we can jump to the end from index
16 // 0.
17 return destination == 0;
18}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected