Just want to share you with my answer, simple c++, 16ms(Greedy)


  • 1
    R
    int jump(vector<int>& nums) {
        int ans=0, pos=0;
        int maxStep;
        while(pos < nums.size()) {
            maxStep = nums[pos];
            if(pos+nums[pos]>=nums.size()-1) return ans+1;
            int max=0, pos_c=pos;
            for(int i=1; i<=maxStep; i++) {
                if(i+nums[pos_c+i] > max) {
                    max = i+nums[pos_c+i];
                    pos = pos_c+i;
                }
            }
            ans++;
        }   
        return ans;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.