Partitioning Into Minimum Number Of Deci-Binary Numbers (LeetCode 1689): Solution and Explanation

Medium
String
Greedy

A deci-binary number uses only the digits 0 and 1. Given a positive integer n as a string, find the fewest deci-binary numbers that add up to n. The answer turns out to be the largest digit in n.

Intuition

Look at one digit position at a time. Every deci-binary number puts either a 0 or a 1 in that position, so adding k of them raises that position by at most k. Carries cannot help either: carries only appear when a position goes past 9, and you are trying to reach a digit that is at most 9. So a position whose digit is d forces you to use at least d numbers.

That gives a lower bound: the answer is at least the largest digit. The surprising part is that the bound is always reachable. Picture each digit as a column of d stacked blocks. Slice the picture into horizontal layers. Layer k has a 1 in every column whose digit is at least k and a 0 elsewhere.

Each layer is a deci-binary number. Its leading digit is a 1 as long as you drop leading zeros, and every layer up to the largest digit is non-empty. Stacking all the layers rebuilds every column exactly, so the layers sum to n. The number of layers equals the tallest column, which is the largest digit.

So the whole problem is a scan for the maximum digit. Since no digit can exceed 9, you can stop as soon as you see one.

Approach: Maximum digit

  1. Set best = 0.
  2. For each character in n, convert it to a digit and update best = max(best, digit).
  3. If best reaches 9, stop scanning because no digit can be larger.
  4. Return best.

Step-by-step example

n = "32"
  1. best starts at 0.
  2. Read 3: best becomes 3.
  3. Read 2: it is smaller, so best stays 3. The scan ends and the function returns 3.
  4. Why 3 is enough: layer 1 is 11, layer 2 is 11, and layer 3 is 10 (only the first column is 3 tall). 11 + 11 + 10 = 32.

Code: Python, Java, C++

1class Solution:
2    def minPartitions(self, n: str) -> int:
3        best = 0
4        for ch in n:
5            best = max(best, int(ch))
6            if best == 9:
7                break
8        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each digit is read once, and the scan may stop early at the first 9.

Space

O(1)

Only the running maximum is stored.

Other approaches

Peel layers explicitly (time O(9n), space O(n))

Repeatedly subtract a deci-binary number that has a 1 in every non-zero position, counting the steps until the number is all zeros. It constructs the actual partition and gives the same count, but it costs O(n times the largest digit).

Built-in max over characters (time O(n), space O(1))

In Python, int(max(n)) returns the same answer in one line, since digit characters compare in numeric order. It still scans the whole string and cannot stop early at 9.

Common mistakes

  • Converting n to an integer. It can have 100,000 digits, which overflows every fixed-width type and is wasteful even with big integers.
  • Comparing characters to integers, for example ch > best where ch is a character. Convert with ch - '0' or int(ch) first.
  • Thinking carries could reduce the count. A carry only happens past 9, which cannot help you reach a single digit.
  • Overthinking zeros. A 0 digit simply means every layer has a 0 in that position.

Frequently asked questions

Why is the answer the largest digit?

A position with digit d needs at least d numbers, because each one adds at most 1 there. And the largest digit is enough: layer k puts a 1 wherever the digit is at least k, and those layers add up to n exactly.

Are the layers really valid deci-binary numbers?

Yes. Each layer uses only 0s and 1s. Once leading zeros are dropped, each layer starts with a 1, and every layer up to the largest digit contains at least one 1, so all of them are positive.

Why can the loop stop at 9?

Digits only go up to 9, so once you see a 9 the maximum cannot grow any further.

What is the time complexity?

O(n) time for a single scan over the digits and O(1) extra space.