-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path045_JumpGameII45.java
88 lines (78 loc) · 2.28 KB
/
045_JumpGameII45.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class Solution {
public int jump(int[] nums) {
int jumps = 0;
for(int i=0; i < nums.length - 1; i++){
jumps++;
int temp = i;
i = findMax(nums, i);
if(i == temp){
break;
}
i--;
}
return jumps;
}
private int findMax(int[] nums, int beg){
int max = 0;
int idx = -1;
for(int i=beg; i <= beg + nums[beg]; i++){
if(i + nums[i] >= nums.length - 1){
return i;
}
//we check the maximum jump which can be reached from the given set of elements
if(i != beg && i + nums[i] >= max + idx){
max = nums[i];
idx = i;
}
}
return idx;
}
}
***********************************************************
class Solution {
public int jump(int[] nums) {
int jumps=0;
int currend=0;
int currfarthest=0;
//nums.length-1 cause we dont need to jump from end index
for(int i=0;i<nums.length-1;i++){
// the farthest we can reach from a index
currfarthest=Math.max(currfarthest,nums[i]+i);
//if we reach end index before reaching currend we already
//know that we should have jumped earlier so we will
//update currend to farthest(i.e end index or greater)
//increment jumps and break
if(i==currend){
// when we reach currentend we need to take another jump
jumps++;
currend=currfarthest;
// break the loop if we already reached end or further
if(currend>=nums.length-1){
break;
}
}
}
return jumps;
}
}
*************************************************
class Solution
{
public int jump(int[] nums)
{
int pos=0;
int reach=0;
int jumps=0;
//last jump is useless so run loop to nums.length-1 only
for(int i=0;i<nums.length-1;i++)
{
reach=Math.max(reach,i+nums[i]);
if(pos==i)
{
pos=reach;
jumps++;
}
}
return jumps;
}
}