55. Jump Game

 

Approach

calculate the maxIndex possible from every point if index is maximum from maxIndex than return false else return true at end

Complexity

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

Java
class Solution {
    public boolean canJump(int[] nums) {
        int maxIndex = 0;
    for(int i = 0; i < nums.length; i++){
        if(maxIndex < i){
            return false;
        }else{
            maxIndex = Math.max(maxIndex,i + nums[i]);
        }
    }
    return true;
    }
}

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix