-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.java
More file actions
55 lines (39 loc) · 1 KB
/
JumpGame.java
File metadata and controls
55 lines (39 loc) · 1 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Jump Game
You are given an integer array nums where each element nums[i] indicates your maximum jump length at that position.
Return true if you can reach the last index starting from index 0, or false otherwise.
Example 1:
Input: nums = [1,2,0,1,0]
Output: true
Explanation: First jump from index 0 to 1, then from index 1 to 3, and lastly from index 3 to 4.
Example 2:
Input: nums = [1,2,1,0,1]
Output: false
Constraints:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
*/
class JumpGame
{
public boolean canJump(int[] nums)
{
int lastElement = nums[0];
int n = nums.length;
for(int i = 0; i < n; i++)
{
if(i > lastElement)
{
return false;
}
else
{
lastElement = Math.max(lastElement, i + nums[i]);
if(lastElement >= n - 1)
{
return true;
}
}
}
return false;
}
}