Posted on

Description

Submission

class Solution {
public:
    bool canJump(vector<int>& nums) {
        if(nums.size() == 1) return true;
        if(nums.size() > 1 && nums[0] == 0) return false;

        int startZero = 0;
        bool isCounting = false;
        for(int i = 0; i < nums.size(); ++i) {
            if(nums[i] == 0) {
                if(i == nums.size() - 1) {
                    if(isCounting) {
                        for(int j = startZero - 1; j >= 0; --j) {
                            if(nums[j] >= i - j) {
                                return true;
                            }
                        }
                        return false;
                    } else {
                        return true;
                    }
                }
                if(!isCounting) startZero  = i;
                isCounting = true;
            } else {
                if(isCounting) {
                    for(int j = startZero - 1; j >= 0; --j) {
                        if(nums[j] >= i - j) {
                            isCounting = false;
                            break;
                        }
                    }
                    if(isCounting) return false;
                }

            }
        }
        return true;
    }
};

Submission 200929

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int far = 0;
        
        for(int i = 0; i < nums.size(); ++i) {
            if(far < i) return false;
            far = max(far, i + nums[i]);
        }
        
        return (far >= nums.size() - 1);
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *