Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Move from brute-force thinking to an efficient approach using greedy strategy.
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 <= 105n consists of only digits.n does not contain any leading zeros and represents a positive integer.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"32"
"82734"
"27346209830709182346"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1689: Partitioning Into Minimum Number Of Deci-Binary Numbers
class Solution {
public int minPartitions(String n) {
int ans = 0;
for (int i = 0; i < n.length(); ++i) {
ans = Math.max(ans, n.charAt(i) - '0');
}
return ans;
}
}
// Accepted solution for LeetCode #1689: Partitioning Into Minimum Number Of Deci-Binary Numbers
func minPartitions(n string) (ans int) {
for _, c := range n {
ans = max(ans, int(c-'0'))
}
return
}
# Accepted solution for LeetCode #1689: Partitioning Into Minimum Number Of Deci-Binary Numbers
class Solution:
def minPartitions(self, n: str) -> int:
return int(max(n))
// Accepted solution for LeetCode #1689: Partitioning Into Minimum Number Of Deci-Binary Numbers
impl Solution {
pub fn min_partitions(n: String) -> i32 {
n.as_bytes().iter().fold(0, |ans, &c| ans.max((c - b'0') as i32))
}
}
// Accepted solution for LeetCode #1689: Partitioning Into Minimum Number Of Deci-Binary Numbers
function minPartitions(n: string): number {
return Math.max(...n.split('').map(Number));
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.