Skip to content

Commit

Permalink
Jump Game II
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranjal authored Oct 10, 2016
1 parent 583b995 commit 35a5e9e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions JumpGameII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size();

if(n == 0 || n == 1){
return 0;
}

vector<int> temp(n, INT_MAX);
temp[0] = 0;
int start = 0;
int i = 1;

while(i < n){
while(nums[start] - i + start < 0 && start < i){
start++;
}
if(start == i){
break;
}
temp[i] = min(temp[i], temp[start] + 1);
i++;
}

return temp[n-1];
}
};

0 comments on commit 35a5e9e

Please sign in to comment.