Skip to content

Latest commit

 

History

History
28 lines (24 loc) · 625 Bytes

File metadata and controls

28 lines (24 loc) · 625 Bytes

55. Jump Game (Medium)

Solution

  • For each index we can calculate how far we can jump.it will be our maximum reach.
  • If at any point we exceed the maximum reach, we can't jump to last index.
  • TC:O(N)
  • SC:O(1)

Code

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