C++ Simple O(n) solution


  • 2
    J
    bool canJump(vector<int>& nums) {
        int remains = nums.front();
        for(int i = 1; i<nums.size(); i++) {
            if(remains <= 0) return false;
            remains = max(--remains, nums[i]);
        }
        return true;
    }

  • 0
    C

    Excellent !!. I would only add a test if (nums.empty()) return true to deal with the especial case.


Log in to reply
 

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