↓Skip to main content
  1. LeetCode/

LeetCode 3697: Compute Decimal Representation

·1 min· ·
LeetCode Easy Math Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-09-28

  • Submissions: Accepted (first try)

Approach
#

A component is “one digit 1–9 × a power of 10”, so each non-zero digit maps to exactly one component, e.g. 537 = 500 + 30 + 7. Zero digits need none. That is the minimum, since each component covers only one digit.

Take digits from the low end with % 10 while tracking the place value place, then reverse into descending order.

My Solution
#

class Solution:
    def decimalRepresentation(self, n: int) -> List[int]:
        res = []
        place = 1
        while n:
            d = n % 10
            if d:
                res.append(d * place)
            n //= 10
            place *= 10
        return res[::-1]                   # descending

I originally built each component as a string f"{r}{'0' * zeros}"; multiplying by place is more direct.

Complexity
#

Time O(d) for d digits, space O(d).

Mistakes to Avoid
#

  • Skip zero digits.
  • Digits come out from the low end, so reverse at the end.

Interview Explanation
#

Each non-zero digit times its place value is one component, and a component can only represent one digit, so this is minimal. I peel digits with % 10, skip zeros, and reverse into descending order. Time O(d).

Related

LeetCode 3550: Smallest Index With Digit Sum Equal to Index
·1 min
LeetCode Easy Math Array
LeetCode 3550 note: check indices in increasing order, computing each digit sum; the first match is the answer
LeetCode 1979: Find Greatest Common Divisor of Array
·1 min
LeetCode Easy Math Number-Theory Gcd Array
LeetCode 1979 note: find the minimum and maximum, then take their GCD with Euclid’s algorithm
LeetCode 1304: Find N Unique Integers Sum up to Zero
·1 min
LeetCode Easy Math Array
LeetCode 1304 note: place 1..n/2 and their negatives so each pair cancels, adding 0 when n is odd
LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 989: Add to Array-Form of Integer
·2 mins
LeetCode Easy Array Math
LeetCode 989 note: treat k as the carry, add from the last element backwards, and keep prepending digits while k remains
LeetCode 908: Smallest Range I
·1 min
LeetCode Easy Array Math
LeetCode 908 note: lower the maximum by k and raise the minimum by k; the answer is max(0, max - min - 2k)