Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created December 3, 2014 06:19
Show Gist options
  • Save rohith2506/0e7ebbf572b3609bbe8e to your computer and use it in GitHub Desktop.
Save rohith2506/0e7ebbf572b3609bbe8e to your computer and use it in GitHub Desktop.
/*
O(n) solution using BFS.
Let's say we have [2,3,1,1,4]. Divide values into nodes such that nodes at level i can be reached by jump from level (i-1)
now didiving into levels, [2 || 3,1 || 1,4]
*/
class Solution {
public:
int jump(int A[], int n) {
if(n < 2) return 0;
int i=0, curmax = 0, nextmax = 0, level = 0;
while ( curmax - i + 1 > 0 ){
level ++;
for(; i<=curmax; i++){
nextmax = max(nextmax, A[i]+i);
if(nextmax >= n-1) return level;
}
if(curmax == nextmax) return INT_MAX;
curmax = nextmax;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment