/
min. no. of jumps.cpp
211 lines (139 loc) Β· 3.82 KB
/
min. no. of jumps.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
//////////////////////////////////////////////////////
//Question/Info
Minimum number of jumps
Medium Accuracy: 32.96% Submissions: 100k+ Points: 4
Given an array of N integers arr[] where each element represents the max number of steps that can be made forward from that element. Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.
Note: Return -1 if you can't reach the end of the array.
Example 1:
Input:
N = 11
arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}
Output: 3
Explanation:
First jump from 1st element to 2nd
element with value 3. Now, from here
we jump to 5th element with value 9,
and from here we will jump to last.
Example 2:
Input :
N = 6
arr = {1, 4, 3, 2, 6, 7}
Output: 2
Explanation:
First we jump from the 1st to 2nd element
and then jump to the last element.
Your task:
You don't need to read input or print anything. Your task is to complete function minJumps() which takes the array arr and it's size N as input parameters and returns the minimum number of jumps. If not possible returns -1.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 β€ N β€ 107
0 β€ arri β€ 107
Company Tags
Adobe Amazon Housing.com Moonfrog Labs Walmart Microsoft Google
author: srj_v
//////////////////////////////////////////////////////
*/
#include <bits/stdc++.h>
using namespace std;
#define _IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long double ld;
typedef long long int lli;
#pragma GCC optimize("Ofast")
void c_p_c()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////////////////////
int32_t main() {
///////////
// c_p_c();
///////////
_IOS
//////////
// code
/*
int t ; cin >> t; while(t--){}
*/
class Solution {
public:
int minJumps(int arr[], int n) {
// Your code here
// like longest increasing subseq...
// vector<int>dp(n,INT_MAX);
// dp[0] = 0;
// if(arr[0]==0) return -1;
// for(int i = 1 ; i < n ;i++){
// for(int j = 0 ; j < i ;j++){
// if(i<=j+arr[j]){
// dp[i] = min(dp[i],dp[j]+1);
// }
// }
// }
// if(dp[n-1] == INT_MAX) dp[n-1] = -1;
// return dp[n-1];
if (n <= 1)
return 0;
if (arr[0] == 0)
return -1;
int maxReach = arr[0];
int step = arr[0];
int jump = 1;
int i = 1;
for (i = 1; i < n; i++) {
if (i == n - 1)
return jump;
maxReach = max(maxReach, i + arr[i]);
step--;
if (step == 0) {
jump++;
if (i >= maxReach)
return -1;
step = maxReach - i;
}
}
return -1;
}
/*
55. Jump Game
Medium
6928
442
Add to List
Share
Given an array of non-negative integers nums, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
Constraints:
1 <= nums.length <= 104
0 <= nums[i] <= 105
/////////////////////////////////////////
class Solution {
public:
bool canJump(vector<int>& nums) {
int reach = 0;
for(int i = 0; i <nums.size();i++){
if(i>reach) return false;
reach = max(reach,i+nums[i]);
}
return true;
}
};
*/
};
// cerr << "time: " << clock() << " ms" << '\n';
return 0;
}
//////////////////////////////////////////////////////