leetcode-study

55. Jump Game

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        """
        Determine whether the last index of the list 'nums' can be reached from the first index.
        
        Each element in 'nums' represents the maximum jump length from that position.
        The function returns True if the end of the list is reachable, otherwise False.
        
        Parameters:
            nums (List[int]): A list of non-negative integers representing maximum jump lengths.
        
        Returns:
            bool: True if the last index is reachable, otherwise False.
        """
        farthest = 0  # This variable tracks the farthest index we can reach so far.
        n = len(nums)  # Total number of positions in the list.
        
        # Iterate through each index in the array.
        for i in range(n):
            # If the current index is greater than the farthest reachable index,
            # it means we are stuck and cannot progress further.
            if i > farthest:
                return False
            
            # Update the farthest reachable index from the current position.
            farthest = max(farthest, i + nums[i])
            
            # If we've already reached or passed the last index, return True.
            if farthest >= n - 1:
                return True
        
        # If the loop completes, check whether the last index was reached.
        return False

Summary of Techniques and Approaches: