Neat linear C++ solution


  • 0
    Q
        bool canJump(vector<int>& nums) {
            if (nums.size() <= 1) return true; 
            int start = 0, end = 0;
            while (1) {
                int max = -1;
                for (int i = start; i <= end; i++) {
                    if (i + nums[i] > max) max = i + nums[i];
                }
                if (max >= nums.size() - 1) return true;
                start = end;
                if (end == max) return false;
                end = max;
            }
            return true;
        }
    

Log in to reply
 

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