{"id":7146,"date":"2020-07-25T15:58:02","date_gmt":"2020-07-25T22:58:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7146"},"modified":"2020-07-25T15:59:01","modified_gmt":"2020-07-25T22:59:01","slug":"leetcode-1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1526. Minimum Number of Increments on Subarrays to Form a Target Array"},"content":{"rendered":"\n<p>Given an array of positive integers&nbsp;<code>target<\/code>&nbsp;and an array&nbsp;<code>initial<\/code>&nbsp;of same size with all zeros.<\/p>\n\n\n\n<p>Return the minimum number of operations to form a&nbsp;<code>target<\/code>&nbsp;array from&nbsp;<code>initial<\/code>&nbsp;if you are allowed to do the following operation:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose&nbsp;<strong>any<\/strong>&nbsp;subarray from&nbsp;<code>initial<\/code>&nbsp;and increment each value by one.<\/li><\/ul>\n\n\n\n<p>The answer is guaranteed to fit within the range of a 32-bit signed integer.<\/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> target = [1,2,3,2,1]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>We need at least 3 operations to form the target array from the initial array.\n[0,0,0,0,0] increment 1 from index 0 to 4&nbsp;(inclusive).\n[1,1,1,1,1] increment 1 from index 1 to 3&nbsp;(inclusive).\n[1,2,2,2,1] increment 1 at index 2.\n[1,2,3,2,1] target array is formed.\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> target = [3,1,1,2]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>(initial)[0,0,0,0] -&gt; [1,1,1,1] -&gt; [1,1,1,2] -&gt; [2,1,1,2] -&gt; [3,1,1,2] (target).\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> target = [3,1,5,4,2]\n<strong>Output:<\/strong> 7\n<strong>Explanation: <\/strong>(initial)[0,0,0,0,0] -&gt; [1,1,1,1,1] -&gt; [2,1,1,1,1] -&gt; [3,1,1,1,1] \n                                  -&gt; [3,1,2,2,2] -&gt; [3,1,3,3,2] -&gt; [3,1,4,4,2] -&gt; [3,1,5,4,2] (target).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> target = [1,1,1,1]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= target.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= target[i] &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>If arr[i] &lt; arr[i &#8211; 1], if we can generate arr[i] then we can always generate arr[i] with no extra cost.<br>e.g. [3, 2, 1] 3 &lt; 2 &lt; 1, [0,0,0] =&gt; [1,1,1] =&gt; [2, 2, 1] =&gt; [3, 2, 1]<\/p>\n\n\n\n<p>So we only need to handle the case of arr[i] > arr[i &#8211; 1], we can reuse the computation, with extra cost of arr[i] &#8211; arr[i-1].<br>e.g. [2, 5]: [0,0] => [1, 1] => [2, 2], it takes 2 steps to cover arr[0].<br>[2,2] => [2, 3] => [2, 4] => [2, 5], takes another 3 steps (arr[1] &#8211; arr[0] \/ 5 &#8211; 2) to cover arr[1].<\/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++\">\nclass Solution {\npublic:\n  int minNumberOperations(vector<int>& target) {\n    int ans = target[0];\n    for (int i = 1; i < target.size(); ++i)\n      ans += max(0, target[i] - target[i - 1]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of positive integers&nbsp;target&nbsp;and an array&nbsp;initial&nbsp;of same size with all zeros. Return the minimum number of operations to form a&nbsp;target&nbsp;array from&nbsp;initial&nbsp;if you are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[],"class_list":["post-7146","post","type-post","status-publish","format-standard","hentry","category-array","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7146","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=7146"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7146\/revisions"}],"predecessor-version":[{"id":7148,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7146\/revisions\/7148"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}