Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1689. Partitioning Into Minimum Number Of Deci-Binary Numbers - Leetcode Solution

Code Implementation

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;
};
      

Problem Description

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^5
  • n consists of digits only and does not have leading zeros

Thought Process

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.

Solution Approach

  • Step 1: Recognize that a deci-binary number can only contribute 1 or 0 to each digit's position.
  • Step 2: For any digit 'd' in n, you need at least 'd' deci-binary numbers to reach it, because each can contribute at most 1 at that position.
  • Step 3: Therefore, scan through all digits in n and find the maximum digit.
  • Step 4: The answer is this maximum digit, because that's the minimum number of deci-binary numbers needed to sum up to 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.

Example Walkthrough

Input: n = "27346209830709182346"

  • Scan through each digit: 2, 7, 3, 4, 6, 2, 0, 9, 8, 3, 3, 0, 7, 0, 9, 1, 8, 2, 3, 4, 6
  • The highest digit is 9.
  • This means, at some position, you need at least 9 ones stacked up to reach the digit 9 using deci-binary numbers.
  • Thus, the answer is 9.

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.

Time and Space Complexity

  • Brute-force approach:
    • Would enumerate all possible deci-binary numbers and try to find combinations that sum to n.
    • This is exponential in time and infeasible for large inputs.
  • Optimized approach:
    • Time Complexity: O(L), where L is the length of n. We only scan through the string once.
    • Space Complexity: O(1), since only a variable for the maximum digit is needed (excluding input storage).

Summary

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.