-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjump.java
More file actions
28 lines (28 loc) · 709 Bytes
/
jump.java
File metadata and controls
28 lines (28 loc) · 709 Bytes
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
26
27
28
public class Solution {
public boolean canJump(int[] nums) {
int i = 0;
while(i<nums.length)
{
if(nums[i] == 0&&i!=nums.length-1)
{
return false;
}
int max = Integer.MIN_VALUE;
int next = i+1;
for(int j = 1;j<nums[i]+1;j++)
{
if(i+j>nums.length-1)
{
return true;
}
if(nums[i+j]+j-1>max)
{
max = nums[i+j]+j-1;
next = i+j;
}
}
i = next;
}
return true;
}
}