jump game
class Solution { public: bool canJump(int A[], int n) { int maxi; maxi=A[0]; for(int i=1;i<n-1;i++) { if(maxi<i)return false; if(i+A[i]>maxi) { maxi=i+A[i]; } } if(maxi>=n-1)return true; else return false; } };
jump game II
class Solution { public: int jump(int A[], int n) { int *step=new int[n]; memset(step,0,sizeof(step)); int lasti=0,maxreach=A[0],reachindex; for(int i=1;i<n;i++) { if(lasti+A[lasti]>=i) { step[i]=step[lasti]+1; } else { step[i]=step[reachindex]+1; lasti=reachindex; } if(i+A[i]>maxreach) { maxreach=i+A[i]; reachindex=i; } } return step[n-1]; } };