30 Days of Code [Day 7]
QUESTION : 45. Jump Game II
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fo(i,s,n) for(int i=s;i<n;++i)
#define of(i,s,n) for(int i=s-1;i>=n;--i)
#define fv(V) for( auto &it : V )
class Solution {
public:
int jump(vector<int>& nums) {
int jumps = 0;
int curpos = 0;
int maxpos = 0;
fo(i,0,nums.size()-1){
maxpos = max(nums[i]+i, maxpos);
if(i > curpos)
return 0;
if(i == curpos){
curpos = maxpos;
jumps++;
}
}
return jumps;
}
};