{x}
blog image

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.

Solution Explanation:

This problem asks for the minimum number of deci-binary numbers needed to sum up to a given decimal number represented as a string. A deci-binary number is a number where each digit is either 0 or 1.

The key insight is that the minimum number of deci-binary numbers required is simply the largest digit in the input string n. This is because:

  1. Upper Bound: We can never need fewer deci-binary numbers than the largest digit. If the largest digit is d, we need at least one deci-binary number with d in the same position to achieve the sum.

  2. Sufficiency: We can always achieve the sum using deci-binary numbers based on the maximum digit. For each digit position, we create deci-binary numbers with 1's in that position until we reach the required digit value.

Therefore, the solution involves iterating through the string to find the maximum digit and returning it as the result.

Time and Space Complexity Analysis:

  • Time Complexity: O(n), where n is the length of the input string n. We iterate through the string once to find the maximum digit.

  • Space Complexity: O(1). We only use a constant amount of extra space to store the maximum digit.

Code Implementation in Different Languages:

The code implementations below all follow the same approach: iterate through the input string and find the maximum digit.

Python:

class Solution:
    def minPartitions(self, n: str) -> int:
        return int(max(n))

Java:

class Solution {
    public int minPartitions(String n) {
        int maxDigit = 0;
        for (char c : n.toCharArray()) {
            maxDigit = Math.max(maxDigit, c - '0');
        }
        return maxDigit;
    }
}

C++:

class Solution {
public:
    int minPartitions(string n) {
        int maxDigit = 0;
        for (char c : n) {
            maxDigit = max(maxDigit, c - '0');
        }
        return maxDigit;
    }
};

Go:

func minPartitions(n string) int {
    maxDigit := 0
    for _, c := range n {
        digit := int(c - '0')
        if digit > maxDigit {
            maxDigit = digit
        }
    }
    return maxDigit
}

JavaScript:

/**
 * @param {string} n
 * @return {number}
 */
var minPartitions = function(n) {
    return Math.max(...n.split('').map(Number));
};

TypeScript:

function minPartitions(n: string): number {
    return Math.max(...n.split("").map(Number));
}

C#:

public class Solution {
    public int MinPartitions(string n) {
        int maxDigit = 0;
        foreach (char c in n) {
            maxDigit = Math.Max(maxDigit, c - '0');
        }
        return maxDigit;
    }
}

Rust:

impl Solution {
    pub fn min_partitions(n: String) -> i32 {
        *n.bytes().map(|b| (b - b'0') as i32).max().unwrap()
    }
}

These solutions all have the same time and space complexity as described above. They efficiently find the maximum digit in the input string to determine the minimum number of deci-binary numbers needed.