-
Notifications
You must be signed in to change notification settings - Fork 0
/
Min_Jumps.cpp
92 lines (75 loc) · 1.62 KB
/
Min_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
/*
Given an array of integers where each element represents the max number of steps that can be made forward from that element. Print the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.
Input Format
n, size of array Ai, array elements
Constraints
1<=n<=5000 1<=Ai<=10^5
Output Format
Number of minimum jumps
Sample Input
11
1 3 5 8 9 2 6 7 6 8 9
Sample Output
3
Explanation
3 (1-> 3 -> 8 ->9)
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define all(x) (x).begin(), (x).end()
#define pi pair<int , int>
#define pll pair<ll , ll>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll>>
#define fast ios::sync_with_stdio(0) ; cin.tie(0) ;
#define fastcout ios::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0);
const ll mod = 1000000007;
int doSome(vi &arr , int index ,int mn){
int n = arr.size();
if(index >= n-1){
return mn;
}
int a = INT_MAX;
for(int i = index+1 ; i < index+arr[i] ; ++i){
a = min(a,doSome(arr ,i , mn+1));
}
return a;
}
void solv() {
int n ;
cin >> n ;
vi arr(n);
for(int i = 0 ; i < n ; ++i){
cin >> arr[i];
}
vi dp(n , n+1);
dp[n-1] = 0;
for(int i = n-2; i >= 0 ; --i){
for(int j = 1 ; j <= arr[i] ; ++j){
if(i+j < n){
dp[i] = min({dp[i] , 1 + dp[i+j]});
}else{
break;
}
}
}
// for(auto x : dp){
// cout << x << " ";
// }
// cout << endl;
cout << dp[0] << endl;
// cout << doSome(arr , 0 , 1);
}
int main() {
fast;
int tt;
// cin >> tt;
tt =1;
while (tt--) {
solv();
}
return 0;
}