Easy to understand C++ 13 ms solution, beats 99%


  • 0
    M

    Solution is very straight forward,we want to jump as far as possible.

    class Solution {
    public:
        int jump(vector<int>& nums) {
            int current = 0, step = 0;
            while (current < nums.size() -1) {
                if (nums[current] + current >= nums.size()-1) {
                    step ++;
                    break;
                }
                else {
                    int next = current + 1, nextNext = current + 2, jumpDist = nums[current];
                    for (int i = 1; i <= jumpDist; i++) {
                        int temp = nums[current + i] + (current + i);
                        if (temp > nextNext) {
                            nextNext = temp;
                            next = current + i;
                        }
                    }
                    current = next;
                    step ++;
                    }
            }
            return step;
        }
    };
    

Log in to reply
 

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