This article is participating in Python Theme Month. See the link to the event for more details

describe

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	
Copy the code

Example 2:

Input: n = "82734"
Output: 8
Copy the code

Example 3:

Input: n = "27346209830709182346"
Output: 9
Copy the code

Note:

1 <= n.length <= 10^5
n consists of only digits.
n does not contain any leading zeros and represents a positive integer.
Copy the code

parsing

Given n in decimal notation, let’s find the least number of legal deci-binary. Deci-binary in the problem is a decimal number consisting of a 1 or a 0 that does not start with 0. The given n is a string representing a very large number. The simplest and most brainless way to do this at first is to loop through built-in functions or brute force to find the number of deci-binary numbers that satisfy the problem. However, in example 3, I know that this will not work because the numbers represented by n are too large to be calculated. According to the urine nature of Leetcode, there is usually a pattern to this problem, so finding the minimum number of legal deci-binary requires finding the pattern.

Let’s take n= “82734” as an example. To find the minimum number of legitimate deci-binary, there must be only one combination, as follows:

1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 --------- 8 2 7 3 4Copy the code

As we can see, each number is as large as possible, so that the deci-binary of the last number is as small as possible. When we enumer more examples, we further analyze the composition of all the deci-binary results of each example, and we can find that, The deci-binary number in the result is the same as the largest numeral character in n. The maximum number of characters in n is converted to an integer, which is the answer to this question. No surprise! I’m a fucking genius!

answer

class Solution(object):
    def minPartitions(self, n):
        """
        :type n: str
        :rtype: int
        """
        return max(n)            	      
		
Copy the code

The results

Runtime: 44 ms, Faster than 84.72% of Python online submissions for Partitioning Into Minimum Number of Deci-Binary Numbers. Memory Usage: 10000 MB, less than 77.07% of Python online submissions for Partitioning Into Minimum Number of Binary Numbers.Copy the code

Link: leetcode.com/problems/pa…

Your support is my greatest motivation