-
Notifications
You must be signed in to change notification settings - Fork 0
/
JumpGameII.cpp
59 lines (46 loc) · 975 Bytes
/
JumpGameII.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
/*
*Time Limited Out
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int jump(vector<int>& nums) {
vector<int> allstep;
where(nums,0,0,allstep);
int minstep=allstep.at(0);
cout<<allstep.at(0)<<endl;
for(int i=1;i<allstep.size();i++){
cout<<allstep.at(i)<<endl;
if(allstep.at(i)<minstep){
minstep=allstep.at(i);
}
}
return minstep;
}
void where(vector<int>& nums,int curr,int step,vector<int>& allstep){
if(curr==nums.size()-1){
allstep.push_back(step);
}
else if(curr<nums.size()-1){
int max=nums[curr];
for(int i=1;i<=max;i++){
where(nums,curr+i,step+1,allstep);
}
}
}
};
int main(){
int n;
cin>>n;
vector<int> nums;
for(int i=0;i<n;i++){
int num;
cin>>num;
nums.push_back(num);
}
Solution *solution=new Solution();
cout<<solution->jump(nums);
return 0;
}