-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
40 lines (39 loc) · 1.03 KB
/
Copy pathsolution.java
File metadata and controls
40 lines (39 loc) · 1.03 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
// 45. Jump Game II
// https://leetcode.com/problems/jump-game-ii/
// Medium | Java | Accepted 2026-01-12
// Runtime 17 ms | Memory 47.6 MB
class Solution {
public int jump(int[] nums) {
int ind = 0;
int count = 0;
while(ind<nums.length-1)
{
int jumpDis = nums[ind];
int max = 0;
int newInd = ind;
count++;
for(int i = 0; i<=jumpDis; i++)
{
if(nums[ind+i]+ind+i>=nums.length-1)
{
if(i==0)
{
return count;
}
else
{
return count+1;
}
}
if(ind+i<nums.length && nums[ind+i]+ind+i>max)
{
max = nums[ind+i]+ind+i;
newInd = ind+i;
}
}
ind = newInd;
System.out.println(ind);
}
return count;
}
}