Partitioning into Minimum Number of Deci-Binary Numbers

  25 Dec 2020

By Wen Xu

algorithm

Description

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

After several try, we can observe that the number of deci-binary number needed is the maximum digit in n. So below is the implementation.

class Solution:
    def minPartitions(self, n: str) -> int:
        '''
        82734
        11111 1
        71623
        11111 2
        60512
        10111 3
        50401
        10101 4
        40300
        10100 5
        30200
        10100 6
        20100
        10100 7
        10000
        10000 8
        00000
        '''
        nums = []
        for c in n:
            nums.append(int(c))
        return max(nums)
				
comments powered by Disqus