Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 202 · Two Pointers

Happy Number

LeetCode 202 · ಸುಲಭ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Floyd's cycle detection on the digit-square sequence.

ಸುಲಭHash TableMathTwo PointersFloyd's Cycle Finding Algorithm
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Happy Number (LeetCode 202) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Two Pointers ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಸುಲಭ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Repeatedly replacing a number by the sum of the squares of its digits either reaches 1 or falls into a cycle. Detecting the cycle is the same problem as a linked-list loop: run a slow and a fast pointer over the sequence.

Approach

  1. 1

    get_next(n) sums the squares of the digits.

  2. 2

    slow moves one step, fast moves two.

  3. 3

    Stop when fast hits 1 (happy) or meets slow (cycle).

  4. 4

    First version: recursion with a hard-coded shortcut for small non-happy numbers.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Recursion with shortcutO(log n) per step

Recurse on the digit-square sum.

2
Floyd slow / fastAcceptedO(log n) · O(1) space

Two pointers over the sequence.

Recursion with shortcutPythonO(log n) per step
class Solution:
    def isHappy(self, n: int) -> bool:
        total = 0
        if n==1:
            return True
        if n>1 and n<7:
            return False
        while n>0:
            num = n%10
            total+=num**2
            n=n//10
        return self.isHappy(total)
Floyd slow / fastPythonO(log n) · O(1) space
class Solution:
    def isHappy(self, n: int) -> bool:
        def get_next(n):
            total = 0
        
            while n>0:
                num = n%10
                total+=num**2
                n=n//10
            return total
        slow = n
        fast = get_next(n)
        while fast!=1 and slow!=fast:
            slow = get_next(slow)
            fast = get_next(get_next(fast))
        return True if fast==1 else False
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Happy Number — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Happy Number ಹಿಂದಿನ intuition ಏನು?

Repeatedly replacing a number by the sum of the squares of its digits either reaches 1 or falls into a cycle. Detecting the cycle is the same problem as a linked-list loop: run a slow and a fast pointer over the sequence.

Happy Number (LeetCode 202) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. get_next(n) sums the squares of the digits. 2. slow moves one step, fast moves two. 3. Stop when fast hits 1 (happy) or meets slow (cycle). 4. First version: recursion with a hard-coded shortcut for small non-happy numbers.

Happy Number ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(log n) per step. Space: O(1). (Accepted approach: Floyd slow / fast.)

Happy Number ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

A set of seen values also works, at O(k) memory.

Happy Number ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Recursion with shortcut (O(log n) per step): Recurse on the digit-square sum. Floyd slow / fast (O(log n) · O(1) space): Two pointers over the sequence.