Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1922 · Recursion Basics

Count Good Numbers

LeetCode 1922 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

5^ceil(n/2) · 4^floor(n/2) mod 1e9+7.

நடுத்தரம்MathRecursion
LeetCode இல் திற ↗

நான் Count Good Numbers (LeetCode 1922) ஐ இப்படித் தீர்த்தேன் — Recursion Basics பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Even positions can hold 5 digits (0, 2, 4, 6, 8) and odd positions 4 digits (2, 3, 5, 7), independently. So the count is 5^(even positions) × 4^(odd positions), computed with fast modular exponentiation.

Approach

  1. 1

    Backtracking version: build every string — only works for tiny n.

  2. 2

    Even indices: (n + 1) // 2; odd indices: n // 2.

  3. 3

    Fast power mod 1e9+7 for both, then multiply.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
BacktrackingO(5^n) time

Enumerate every good string.

2
Fast modular powerAcceptedO(log n) time

Counting formula.

BacktrackingPythonO(5^n) time
class Solution:
    def countGoodNumbers(self, n: int) -> int:
        #0,2,4,6,8 - length 1
        #0/2/4/6/8_ 2/3/5/7_ 02,03,05,07 22 23 25 27
        #if index is even pick even no
        #if index is odd pick odd no
        count = 0
        def dfs(index,ds):
            nonlocal count
            if len(ds)==n:
                count+=1
                return
            if index%2==0:
                nums = ["0","2","4","6","8"]
                for num in nums:
                    ds.append(num)
                    dfs(index+1,ds)
                    ds.pop()
            elif index%2==1:
                nums = ["2","3","5","7"]
                for num in nums:
                    ds.append(num)
                    dfs(index+1,ds)
                    ds.pop()
        dfs(0,[])
        return count
Fast modular powerPythonO(log n) time
class Solution:
    def countGoodNumbers(self, n: int) -> int:
        #0,2,4,6,8 - 5 at even indices
        #2,3,5,7  - 4 at odd indices
        #01234 - a number of length 5 has 5+1/2 even indices - 3 indices[0,2,4], 5//2 - 2 odd indices - 1,3
        #total of good numbers of string of length n is - 5^(n+1)//2*4^(n//2)
        mod = 10**9+7
        def calculate_pow(x,y):
            x=x%mod
            ans = 1
            while y>0:
                if y%2==1:
                    ans = (ans*x)%mod
                    y = y-1
                else:
                    x = (x*x)%mod
                    y = y//2
            return ans
        return (calculate_pow(5,(n+1)//2)*calculate_pow(4,n//2))%mod
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Count Good Numbers — அடிக்கடி கேட்கப்படும் கேள்விகள்

Count Good Numbers பின்னாலுள்ள intuition என்ன?

Even positions can hold 5 digits (0, 2, 4, 6, 8) and odd positions 4 digits (2, 3, 5, 7), independently. So the count is 5^(even positions) × 4^(odd positions), computed with fast modular exponentiation.

Count Good Numbers (LeetCode 1922) ஐ எப்படித் தீர்ப்பது?

1. Backtracking version: build every string — only works for tiny n. 2. Even indices: (n + 1) // 2; odd indices: n // 2. 3. Fast power mod 1e9+7 for both, then multiply.

Count Good Numbers இன் time மற்றும் space complexity என்ன?

Time: O(log n). Space: O(1). (Accepted approach: Fast modular power.)

Count Good Numbers இல் பொதுவான தவறுகள் என்ன?

n can be 10¹⁵ — exponentiation by squaring is required. The brute-force version doesn't take the modulo, so it disagrees for large n.

Count Good Numbers ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Backtracking (O(5^n) time): Enumerate every good string. Fast modular power (O(log n) time): Counting formula.