Count commas in range II

2026. 3. 15. 13:50Algorithm/Leetcode, Lintcode, HackerRank, etc.

    목차
반응형

n = 1002 -> 1,000 1,001 1,002 -> return 3

class Solution:
    def countCommas(self, n: int) -> int:
        cur = n

        res = 0
        bounds = [1_000, 1_000_000, 1_000_000_000, 1_000_000_000_000, 1_000_000_000_000_000]

        for i, bottom in enumerate(bounds):
            if cur < bottom:
                break
            up_bound = bounds[i + 1] - 1 if i + 1 < len(bounds) else cur
            top = min(cur, up_bound)

            res += (top - bottom + 1) * (i + 1)

        return res
반응형

'Algorithm > Leetcode, Lintcode, HackerRank, etc.' 카테고리의 다른 글

Sum of GCD of Formed Pairs  (0) 2026.03.15
First Unique Even Element  (0) 2026.03.15
Count Commas in Range  (0) 2026.03.15
Longest Univalve path  (0) 2026.03.15
Implement Trie  (0) 2026.03.15