[Geeks for Geeks] Jump Game

Jump Game

Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int canReach(int A[], int N) {
int res = 0;

for(int i = 0; i < N and i <= res; i++) {
res = max(res, i + A[i]);
}

return res >= N - 1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/25/PS/GeeksforGeeks/jump-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.