Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers

[LeetCode Link](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/)

Difficulty: Medium
Topics: String, Greedy
Acceptance Rate: 89.0%

## Hints

### Hint 1

Think about what a deci-binary number can contribute to each digit position. Since each digit in a deci-binary number is either 0 or 1, what does that tell you about how many deci-binary numbers you need to "build up" a particular digit?

### Hint 2

Consider each digit of `n` independently. If a digit is `d`, you need at least `d` deci-binary numbers that have a `1` in that position. Can you think about the problem as: what is the bottleneck digit that requires the most deci-binary numbers?

### Hint 3

The answer is simply the maximum digit in `n`. Each deci-binary number contributes at most 1 to each digit position, so the digit with the largest value dictates the minimum count. And you can always construct exactly that many deci-binary numbers to cover all positions — just "peel off" one layer at a time by placing a 1 wherever the remaining digit is still positive.

## Approach

The key insight is that each deci-binary number has digits that are either 0 or 1. When we sum multiple deci-binary numbers, each digit position accumulates independently (no carries needed if we think of it digit-by-digit).

For a digit `d` at some position in `n`, we need exactly `d` deci-binary numbers to have a `1` in that position. The remaining deci-binary numbers will have a `0` there.

Since every digit position must be satisfied, the total number of deci-binary numbers we need is determined by the position that demands the most — i.e., the maximum digit in `n`.

**Example walkthrough with n = "32":**
- Digit `3` requires 3 deci-binary numbers with a `1` in the tens place.
- Digit `2` requires 2 deci-binary numbers with a `1` in the ones place.
- Maximum digit is `3`, so we need 3 deci-binary numbers.
- Construction: `10 + 11 + 11 = 32` — the first number has a `1` only in the tens place, while the second and third have `1`s in both places.

**Algorithm:**
1. Iterate through each character in the string `n`.
2. Track the maximum digit value seen.
3. Return the maximum digit value.

## Complexity Analysis

Time Complexity: O(n) — single pass through the string where n is the number of digits.
Space Complexity: O(1) — only a single variable to track the maximum.

## Edge Cases

- **Single digit (e.g., "1"):** The answer is that digit itself. The simplest case.
- **All same digits (e.g., "111"):** The answer equals that repeated digit. Every deci-binary number used contributes a `1` everywhere.
- **Contains a 9:** The answer is 9. This is the theoretical maximum since digits range from 0–9.
- **Very long string:** The algorithm handles strings up to 10^5 characters efficiently in O(n) time.
- **String like "10":** The max digit is 1, so only one deci-binary number is needed — the number itself.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
number: "1689"
frontend_id: "1689"
title: "Partitioning Into Minimum Number Of Deci-Binary Numbers"
slug: "partitioning-into-minimum-number-of-deci-binary-numbers"
difficulty: "Medium"
topics:
- "String"
- "Greedy"
acceptance_rate: 8901.3
is_premium: false
created_at: "2026-03-01T03:27:02.039910+00:00"
fetched_at: "2026-03-01T03:27:02.039910+00:00"
link: "https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/"
date: "2026-03-01"
---

# 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers

A decimal number is called **deci-binary** if each of its digits is either `0` or `1` without any leading zeros. For example, `101` and `1100` are **deci-binary** , while `112` and `3001` are not.

Given a string `n` that represents a positive decimal integer, return _the**minimum** number of positive **deci-binary** numbers needed so that they sum up to _`n` _._



**Example 1:**


**Input:** n = "32"
**Output:** 3
**Explanation:** 10 + 11 + 11 = 32


**Example 2:**


**Input:** n = "82734"
**Output:** 8


**Example 3:**


**Input:** n = "27346209830709182346"
**Output:** 9




**Constraints:**

* `1 <= n.length <= 105`
* `n` consists of only digits.
* `n` does not contain any leading zeros and represents a positive integer.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

// Greedy: the minimum number of deci-binary numbers needed equals the
// maximum digit in n, since each deci-binary number contributes at most
// 1 to each digit position.
func minPartitions(n string) int {
maxDigit := 0
for _, ch := range n {
d := int(ch - '0')
if d > maxDigit {
maxDigit = d
}
if maxDigit == 9 {
break
}
}
return maxDigit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "testing"

func TestMinPartitions(t *testing.T) {
tests := []struct {
name string
input string
expected int
}{
{"example 1: two digits summing to 32", "32", 3},
{"example 2: five digits with max 8", "82734", 8},
{"example 3: large number with max digit 9", "27346209830709182346", 9},
{"edge case: single digit 1", "1", 1},
{"edge case: single digit 9", "9", 9},
{"edge case: all ones", "111", 1},
{"edge case: number is itself deci-binary", "10", 1},
{"edge case: all nines", "999", 9},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := minPartitions(tt.input)
if result != tt.expected {
t.Errorf("minPartitions(%q) = %d, want %d", tt.input, result, tt.expected)
}
})
}
}