{"id":1917,"date":"2016-03-28T21:47:46","date_gmt":"2016-03-28T13:47:46","guid":{"rendered":"http:\/\/boweihe.me\/?p=1917"},"modified":"2016-03-28T21:47:46","modified_gmt":"2016-03-28T13:47:46","slug":"leetcode-330-patching-array","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1917","title":{"rendered":"LeetCode 330. Patching Array"},"content":{"rendered":"<h3>\u9898\u76ee<\/h3>\n<p>Given a sorted positive integer array <i>nums<\/i> and an integer <i>n<\/i>, add\/patch elements to the array such that any number in range <code>[1, n]<\/code> inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.<br \/>\n<strong>Example 1:<\/strong><br \/>\n<i>nums<\/i> = <code>[1, 3]<\/code>, <i>n<\/i> = <code>6<\/code><br \/>\nReturn <code>1<\/code>.<br \/>\nCombinations of <i>nums<\/i> are <code>[1], [3], [1,3]<\/code>, which form possible sums of: <code>1, 3, 4<\/code>.<br \/>\nNow if we add\/patch <code>2<\/code> to <i>nums<\/i>, the combinations are: <code>[1], [2], [3], [1,3], [2,3], [1,2,3]<\/code>.<br \/>\nPossible sums are <code>1, 2, 3, 4, 5, 6<\/code>, which now covers the range <code>[1, 6]<\/code>.<br \/>\nSo we only need <code>1<\/code> patch.<br \/>\n<strong>Example 2:<\/strong><br \/>\n<i>nums<\/i> = <code>[1, 5, 10]<\/code>, <i>n<\/i> = <code>20<\/code><br \/>\nReturn <code>2<\/code>.<br \/>\nThe two patches can be <code>[2, 4]<\/code>.<br \/>\n<strong>Example 3:<\/strong><br \/>\n<i>nums<\/i> = <code>[1, 2, 2]<\/code>, <i>n<\/i> = <code>5<\/code><br \/>\nReturn <code>0<\/code>.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u8bf4\u5b9e\u8bdd\u8fd9\u9053\u9898\u6ca1\u6709\u60f3\u51fa\u6765\uff0c\u8fd8\u4ee5\u4e3a\u662f\u4e2a\u6392\u5217\u7ec4\u5408\u7684\u95ee\u9898\uff0c\u540e\u6765\u770b\u4e86\u89e3\u91ca\u624d\u604d\u7136\u5927\u609f\u3002\u7f51\u4e0a\u7248\u672c\u6709\u5f88\u591a\u79cd\uff0c\u6211\u770b\u7684\u5b98\u7f51discussion\u91cc\u5934\u7684\uff08<a href=\"https:\/\/leetcode.com\/discuss\/82822\/solution-explanation\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u91cc<\/a>\uff09\u3002<br \/>\n\u601d\u8def\u5c31\u662f\u8d2a\u5fc3\u6cd5\uff0c\u5047\u8bbe\u76ee\u6807\u662f\u8986\u76d6[0,n]\uff0c\u5f53\u524d\u6211\u4eec\u7684\u7ec4\u5408\u80fd\u591f\u8986\u76d6[0,miss)\u7684\u6570\u5b57\uff08\u6ce8\u610f\u8fd9\u91cc\u7684miss\u662f\u76ee\u524d\u65e0\u6cd5\u5230\u8fbe\u7684\u76ee\u6807\uff0c\u6211\u4eec\u5f97\u7528\u8d2a\u5fc3\u7684\u601d\u60f3\u8ba9\u8fd9\u4e2a\u76ee\u6807\u53ef\u8fbe\uff09\uff0c\u5e76\u4e14\u76ee\u524d\u6b63\u5728\u5904\u7406\u6709\u5e8f\u6570\u7ec4\u4e2dnum[i]\u7684\u6570\uff0c\u90a3\u4e48\u6709\u4e24\u79cd\u60c5\u51b5\u9700\u8981\u8003\u8651\uff1a<\/p>\n<ul>\n<li>\u5982\u679cnum[i]\u6bd4miss\u6765\u7684\u5c0f\uff0c\u8bf4\u660e\u603b\u6709\u4e00\u4e2a\u4e4b\u524d\u6709\u7684\u6570\uff0c\u52a0\u4e0a\u4e86num[i]\u4ee5\u540e\u80fd\u591f\u5230\u8fbemiss\u5b8c\u6210\u4efb\u52a1\u3002\u56e0\u4e3a\u6211\u4eec\u6bd4\u8f83\u8d2a\u5fc3\uff0c\u60f3\u8ba9\u53ef\u8986\u76d6\u7684\u6570\u8d8a\u591a\u8d8a\u597d\uff0c\u6240\u4ee5\u628anum[i]\u5e26\u4e0a\uff0c\u5f97\u5230\u6700\u5927\u53ef\u4ee5\u8986\u76d6\u7684\u8303\u56f4[0,miss+num[i])\uff1b<\/li>\n<li>\u5982\u679cnum[i]\u6bd4miss\u6765\u7684\u5927\uff0c\u90a3\u4e48\u663e\u7136\u628anum[i]\u52a0\u8fdb\u53bb\u662f\u65e0\u6cd5\u5f97\u5230miss\u7684\u3002\u6bd4\u5982\uff1a\u76ee\u524d\u67090,1,2,&#8230;,9\u53ef\u4ee5\u8986\u76d6(\u5373miss=10)\uff0c\u6765\u4e86\u4e2anum[i]=12\u7684\uff0c\u663e\u7136\u52a0\u516512\u5e76\u4e0d\u80fd\u89e3\u51b3\u8986\u76d610\u7684\u95ee\u9898\uff08\u8bd5\u60f3\u4e00\u4e0b0+12\u90fd\u6bd410\u6765\u7684\u5927\uff09\u3002\u6240\u4ee5\u6700\u7ecf\u6d4e\u5b9e\u60e0\u7684\u529e\u6cd5\u5c31\u662f\u628amiss\u81ea\u5df1\u52a0\u8fdb\u53bb\uff0c\u8fd9\u6837\u8fd8\u80fd\u628a\u8986\u76d6\u8303\u56f4\u62d3\u5c55\u5230[0,miss*2)\uff1b<\/li>\n<\/ul>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int minPatches(vector&lt;int&gt;&amp; nums, int n) {\n        long known_sum = 1, count = 0, i = 0;\n        while(known_sum &lt;= n){\n            if(i &lt; nums.size() &amp;&amp; nums[i]&lt;=known_sum){\n                \/\/If we have a choice to expand the range\n                known_sum += nums[i++]; \/\/The best choice is to add nums[i] in, which can reach up to (known+nums[i])\n            } else {\n                \/\/Or we have to add in one item to make the best range, which means to double the range to 2*known_sum\n                known_sum += known_sum;\n                count++;\n            }\n        }\n        return count;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee Given a sorted positive integer array nums and an integer n, add\/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required. Example 1: nums = [1, 3], n = 6 Return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,200],"class_list":["post-1917","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-200"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1917"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1917\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}