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

Class JumpToTheEnd

java/Greedy/JumpToTheEnd.java:1–18  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected