C++ Greedy solution


  • 0
    int jump(vector<int>& nums) {
        int i = 0, next = 0, steps = 0;
        while( i < nums.size()-1 ) {
            steps++; next++;
            int stride = 0;
            if( i + nums[i] >= nums.size() - 1 ) break;
            for( int j=1; j<=nums[i]; j++ ) {
                if( j + nums[j+i] > stride ) {
                    stride = j + nums[j+i];
                    next = j+i;
                }
            }
            i = next;
        }
        return steps;
    }

Log in to reply
 

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