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.