leetcode-study

class Solution:
    def isHappy(self, n: int) -> bool:
        """
        Determine if the given number n is a happy number.
        
        A happy number is defined by the process where:
          1. Replace the number with the sum of the squares of its digits.
          2. Repeat until the number equals 1 (happy) or it loops endlessly in a cycle that does not include 1.
        
        Parameters:
            n (int): The integer to test.
            
        Returns:
            bool: True if n is a happy number, otherwise False.
        """
        def get_next(number: int) -> int:
            # Calculate the sum of the squares of digits of 'number'
            total_sum = 0
            while number > 0:
                digit = number % 10  # Extract the last digit
                total_sum += digit ** 2  # Square it and add to the sum
                number //= 10  # Remove the last digit
            return total_sum
        
        slow = n               # Initialize slow pointer at n
        fast = get_next(n)     # Initialize fast pointer one step ahead
        
        # Use Floyd's Cycle Detection Algorithm: Move slow pointer one step and fast pointer two steps at a time.
        while fast != 1 and slow != fast:
            slow = get_next(slow)         # Move slow pointer by one iteration
            fast = get_next(get_next(fast))  # Move fast pointer by two iterations
        
        # If the fast pointer reaches 1, n is a happy number.
        return fast == 1

Summary of Techniques and Approaches: