{"id":7740,"date":"2020-11-29T11:22:24","date_gmt":"2020-11-29T19:22:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7740"},"modified":"2020-12-01T23:29:21","modified_gmt":"2020-12-02T07:29:21","slug":"leetcode-1674-minimum-moves-to-make-array-complementary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1674-minimum-moves-to-make-array-complementary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1674. Minimum Moves to Make Array Complementary"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1674. Minimum Moves to Make Array Complementary - \u5237\u9898\u627e\u5de5\u4f5c EP373\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/HvMhvPKjCSU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<strong>even<\/strong>&nbsp;length&nbsp;<code>n<\/code>&nbsp;and an integer&nbsp;<code>limit<\/code>. In one move, you can replace any integer from&nbsp;<code>nums<\/code>&nbsp;with another integer between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>limit<\/code>, inclusive.<\/p>\n\n\n\n<p>The array&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>complementary<\/strong>&nbsp;if for all indices&nbsp;<code>i<\/code>&nbsp;(<strong>0-indexed<\/strong>),&nbsp;<code>nums[i] + nums[n - 1 - i]<\/code>&nbsp;equals the same number. For example, the array&nbsp;<code>[1,2,3,4]<\/code>&nbsp;is complementary because for all indices&nbsp;<code>i<\/code>,&nbsp;<code>nums[i] + nums[n - 1 - i] = 5<\/code>.<\/p>\n\n\n\n<p>Return the&nbsp;<em><strong>minimum<\/strong>&nbsp;number of moves required to make&nbsp;<\/em><code>nums<\/code><em>&nbsp;<strong>complementary<\/strong><\/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> nums = [1,2,4,3], limit = 4\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed).\nnums[0] + nums[3] = 1 + 3 = 4.\nnums[1] + nums[2] = 2 + 2 = 4.\nnums[2] + nums[1] = 2 + 2 = 4.\nnums[3] + nums[0] = 3 + 1 = 4.\nTherefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.\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> nums = [1,2,2,1], limit = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 &gt; limit.\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> nums = [1,2,1,2], limit = 2\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> nums is already complementary.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>2 &lt;= n&nbsp;&lt;=&nbsp;10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i]&nbsp;&lt;= limit &lt;=&nbsp;10<sup>5<\/sup><\/code><\/li><li><code>n<\/code>&nbsp;is even.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sweep Line \/ Prefix Sum<\/strong><\/h2>\n\n\n\n<p>Let a = min(nums[i], nums[n-i-1]), b = max(nums[i], nums[n-i-1])<br>The key to this problem is how many moves do we need to make a + b == T.<\/p>\n\n\n\n<p>if 2 &lt;= T &lt; a + 1, two moves, lower both a and b.<br>if a +1 &lt;= T &lt; a + b, one move, lower b<br>if a + b == T, zero move<br>if a + b + 1 &lt;= T &lt; b + limit + 1, one move, increase a<br>if b + limit + 1 &lt;= T &lt;= 2*limit, two moves, increase both a and b.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-1.png\" alt=\"\" class=\"wp-image-7759\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-2.png\" alt=\"\" class=\"wp-image-7760\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-3.png\" alt=\"\" class=\"wp-image-7761\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/12\/1674-ep373-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(n + limit) or O(nlogn) if limit &gt;&gt;n<br>Space complexity: O(limit) or O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minMoves(vector<int>& nums, int limit) {\n    const int n = nums.size();\n    vector<int> delta(limit * 2 + 2);\n    for (int i = 0; i < n \/ 2; ++i) {\n      const int a = min(nums[i], nums[n - i - 1]);\n      const int b = max(nums[i], nums[n - i - 1]);\n      delta[2] += 2;          \/\/ dec a, dec b\n      --delta[a + 1];         \/\/ dec a \n      --delta[a + b];         \/\/ no op\n      ++delta[a + b + 1];     \/\/ inc a\n      ++delta[b + limit + 1]; \/\/ inc a, inc b     \n    }\n    int ans = n;\n    for (int t = 2, cur = 0; t <= limit * 2; ++t) {\n      cur += delta[t];\n      ans = min(ans, cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;of&nbsp;even&nbsp;length&nbsp;n&nbsp;and an integer&nbsp;limit. In one move, you can replace any integer from&nbsp;nums&nbsp;with another integer between&nbsp;1&nbsp;and&nbsp;limit, inclusive. The array&nbsp;nums&nbsp;is&nbsp;complementary&nbsp;if for all&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[673,82,31,177],"class_list":["post-7740","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-differential-array","tag-hashtable","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7740","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=7740"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7740\/revisions"}],"predecessor-version":[{"id":7764,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7740\/revisions\/7764"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}