LeetCode Logo Problem List 0 avatar Premium Debugging... Debugging... Run Description…
- User
LeetCode Logo Problem List 0
avatar Premium Debugging... Debugging...
Run Description Editorial Editorial Solutions Solutions Submissions Submissions Code Testcase Test Result Test Result 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers Medium Topics Companies Hint 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. Seen this question in a real interview before? 1/5 Yes No Accepted 228.4K Submissions 257.4K Acceptance Rate 88.7% Topics Companies Hint 1 Hint 2 Hint 3 Discussion (58)
Choose a type
Copyright ©️ 2024 LeetCode All rights reserved
2.4K
58
C Auto
123 int minPartitions(char* n) {
} Saved Case 1 Case 2 Case 3
n = "32" 1 "32" "82734" "27346209830709182346"
Tag int minPartitions(char* n) {
}
- Assistant
To solve this problem, we need to find the minimum number of positive deci-binary numbers that sum up to the given number
n. A key observation is that the minimum number is equal to the maximum digit in the stringn. This is because each deci-binary number can contribute at most1to each digit's place when summed.Here's how you can implement this in C:
int minPartitions(char* n) { int max_digit = 0; for (int i = 0; n[i] != '\0'; i++) { int digit = n[i] - '0'; if (digit > max_digit) { max_digit = digit; // Since the digits range from '0' to '9', // if we find a '9', we can't find any larger digit. if (max_digit == 9) { break; } } } return max_digit; }