My 16ms greedy solution, a little different from others


  • 0
    P
    int jump(vector<int>& nums) {
        int len = nums.size();
        vector<int> minJumps(len,INT_MAX);
        minJumps[0] = 0;
        for (int i = 0;i < len;++i) {
            if (nums[i] == INT_MAX) return -1;
            for (int j = min(len-1,i+nums[i]);j > i;--j)
                if (minJumps[j] <= minJumps[i]+1)
                    break;
                else
                    minJumps[j] = minJumps[i]+1;
        }
        return minJumps.back();
    }

Log in to reply
 

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