Share my O(n) simple solution


  • 0
    class Solution {
    public:
        bool canJump(vector<int>& nums) {
            bool canJ[nums.size()];
            canJ[nums.size()-1]={true};
            int maxReach=1;
            for(int i=nums.size()-2;i>=0;i--) {
                if(nums[i]>=maxReach) {
                    canJ[i]=true;
                    maxReach=1;
                } 
                else {
                    canJ[i]=false;
                    maxReach++;
                }
            }
            return canJ[0];
        }
    };
    

Log in to reply
 

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