-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumpGame.c
25 lines (23 loc) · 851 Bytes
/
jumpGame.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdbool.h>
bool canJump(int* nums, int numsSize) {
bool res = true;
int current_furthest = nums[0];
if (numsSize == 1) {return res;}
for (int i = 0; i < numsSize; i++) {
if (current_furthest + 1 >= numsSize){return true;} // If we jump pass the whole array
res = (nums[current_furthest] == 0) ? false : true; // Furthest jump lands on 0 making res = false until there is a
// further jump
if (i == current_furthest && res == false){break;} // If we have reached 0 and still cannot jump across it
if (nums[i] + i > current_furthest) {
current_furthest = nums[i] + i;
}
}
return res;
}
int main() {
int numsSize = 3;
int nums[] = {2,0,0};
bool res = canJump(nums, numsSize);
printf("Can reach last index: %d \n", res);
}