class Solution:
def minPartitions(self, n: str) -> int:
# The answer is the maximum digit in n
return int(max(n))
class Solution {
public:
int minPartitions(string n) {
char maxDigit = '0';
for (char c : n) {
if (c > maxDigit) maxDigit = c;
}
return maxDigit - '0';
}
};
class Solution {
public int minPartitions(String n) {
char maxDigit = '0';
for (char c : n.toCharArray()) {
if (c > maxDigit) maxDigit = c;
}
return maxDigit - '0';
}
}
var minPartitions = function(n) {
let maxDigit = 0;
for (let i = 0; i < n.length; i++) {
maxDigit = Math.max(maxDigit, Number(n[i]));
}
return maxDigit;
};
Given a string n representing a large positive integer, the task is to determine the minimum number of positive deci-binary numbers needed to sum up to n.
A deci-binary number is a number where each digit is either 0 or 1, and it does not have leading zeros.
For example, 101 and 1100 are deci-binary, but 112 or 012 are not.
You must find the smallest count of such deci-binary numbers whose sum, when added digit-wise, equals n. There is always exactly one valid answer for each input, and you do not reuse deci-binary numbers; you only need the count.
Constraints:
1 <= n.length <= 10^5n consists of digits only and does not have leading zeros
At first glance, you might consider constructing all possible deci-binary numbers that add up to n, and count the minimum needed.
This would involve breaking down n digit by digit, trying to "cover" each digit with as few deci-binary numbers as possible by simulating the addition process.
However, this brute-force approach is inefficient, especially for large inputs.
By examining how addition works digit-wise, we notice that each digit in n must be formed by adding up 1s from the deci-binary numbers at that position.
The largest digit in n determines how many deci-binary numbers are needed, because you cannot form a digit 'd' using fewer than 'd' ones at that position.
This leads to a much simpler observation: the answer is simply the maximum digit present in n.
n, you need at least 'd' deci-binary numbers to reach it, because each can contribute at most 1 at that position.
n and find the maximum digit.
n.
This approach is efficient because it only requires a single pass through the string, and at each step, you just compare the current digit to a running maximum.
Input: n = "27346209830709182346"
Why? Because you can construct the sum by stacking 9 deci-binary numbers, each with a '1' in the columns where needed, and zeros elsewhere, so that their sum matches each digit of n.
n.n. We only scan through the string once.
The key insight is that the minimum number of deci-binary numbers needed to sum up to n is simply the largest digit in n.
This elegant solution leverages the constraints of deci-binary numbers and the properties of digit-wise addition, reducing the problem to a simple scan for the maximum digit.
This makes the solution both efficient and easy to implement, even for very large numbers.