{"id":7803,"date":"2020-12-13T13:54:18","date_gmt":"2020-12-13T21:54:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7803"},"modified":"2020-12-13T14:02:21","modified_gmt":"2020-12-13T22:02:21","slug":"leetcode-1689-partitioning-into-minimum-number-of-deci-binary-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1689-partitioning-into-minimum-number-of-deci-binary-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers"},"content":{"rendered":"\n<p>A decimal number is called&nbsp;<strong>deci-binary<\/strong>&nbsp;if each of its digits is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>&nbsp;without any leading zeros. For example,&nbsp;<code>101<\/code>&nbsp;and&nbsp;<code>1100<\/code>&nbsp;are&nbsp;<strong>deci-binary<\/strong>, while&nbsp;<code>112<\/code>&nbsp;and&nbsp;<code>3001<\/code>&nbsp;are not.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>n<\/code>&nbsp;that represents a positive decimal integer, return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of positive&nbsp;<strong>deci-binary<\/strong>&nbsp;numbers needed so that they sum up to&nbsp;<\/em><code>n<\/code><em>.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = \"32\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> 10 + 11 + 11 = 32\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = \"82734\"\n<strong>Output:<\/strong> 8\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = \"27346209830709182346\"\n<strong>Output:<\/strong> 9\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>n<\/code>&nbsp;consists of only digits.<\/li><li><code>n<\/code>&nbsp;does not contain any leading zeros and represents a positive integer.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Return the max digit<\/strong><\/h2>\n\n\n\n<p>Proof: For a given string, we find the maximum number m, we create m binary strings.<br>for each one, check each digit, if it&#8217;s greater than 0, we mark 1 at that position and decrease the digit by 1.<\/p>\n\n\n\n<p>e.g. 21534<br>max is 5, we need five binary strings.<br>1. 11111: 21534 -> 10423<br>2. 10111: 10423 -> 00312<br>3: 00111: 00312 -> 00201<br>4: 00101: 00201 -> 00100<br>5: 00100: 00100 -> 00000<\/p>\n\n\n\n<p>We can ignore the leading zeros.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minPartitions(string n) {\n    return *max_element(begin(n), end(n)) - '0';\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A decimal number is called&nbsp;deci-binary&nbsp;if each of its digits is either&nbsp;0&nbsp;or&nbsp;1&nbsp;without any leading zeros. For example,&nbsp;101&nbsp;and&nbsp;1100&nbsp;are&nbsp;deci-binary, while&nbsp;112&nbsp;and&nbsp;3001&nbsp;are not. Given a string&nbsp;n&nbsp;that represents a positive decimal&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,376],"class_list":["post-7803","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7803"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7803\/revisions"}],"predecessor-version":[{"id":7807,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7803\/revisions\/7807"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}