{"id":9496,"date":"2022-02-13T05:44:11","date_gmt":"2022-02-13T13:44:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9496"},"modified":"2022-02-13T05:45:16","modified_gmt":"2022-02-13T13:45:16","slug":"leetcode-2170-minimum-operations-to-make-the-array-alternating","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2170-minimum-operations-to-make-the-array-alternating\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2170. Minimum Operations to Make the Array Alternating"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array&nbsp;<code>nums<\/code>&nbsp;consisting of&nbsp;<code>n<\/code>&nbsp;positive integers.<\/p>\n\n\n\n<p>The array&nbsp;<code>nums<\/code>&nbsp;is called&nbsp;<strong>alternating<\/strong>&nbsp;if:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums[i - 2] == nums[i]<\/code>, where&nbsp;<code>2 &lt;= i &lt;= n - 1<\/code>.<\/li><li><code>nums[i - 1] != nums[i]<\/code>, where&nbsp;<code>1 &lt;= i &lt;= n - 1<\/code>.<\/li><\/ul>\n\n\n\n<p>In one&nbsp;<strong>operation<\/strong>, you can choose an index&nbsp;<code>i<\/code>&nbsp;and&nbsp;<strong>change<\/strong>&nbsp;<code>nums[i]<\/code>&nbsp;into&nbsp;<strong>any<\/strong>&nbsp;positive integer.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum number of operations<\/strong>&nbsp;required to make the array alternating<\/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 = [3,1,3,2,4,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\nOne way to make the array alternating is by converting it to [3,1,3,<strong>1<\/strong>,<strong>3<\/strong>,<strong>1<\/strong>].\nThe number of operations required in this case is 3.\nIt can be proven that it is not possible to make the array alternating in less than 3 operations. \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,2,2]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nOne way to make the array alternating is by converting it to [1,2,<strong>1<\/strong>,2,<strong>1<\/strong>].\nThe number of operations required in this case is 2.\nNote that the array cannot be converted to [<strong>2<\/strong>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Count and sort the frequency of numbers at odd and even positions.<\/p>\n\n\n\n<p>Case 1: top frequency numbers are different, change the rest of numbers to them respectively.<br>Case 2: top frequency numbers are the same, compare top 1 odd + top 2 even vs top 2 even + top 1 odd.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: 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 minimumOperations(vector<int>& nums) {\n    const int n = nums.size();\n    if (n == 1) return 0;\n    unordered_map<int, int> odd, even;\n    for (int i = 0; i < n; ++i)\n      ((i &#038; 1 ? odd : even)[nums[i]])++;\n    vector<pair<int, int>> o, e;\n    for (const auto [k, v] : odd)\n      o.emplace_back(v, k);\n    for (const auto [k, v] : even)\n      e.emplace_back(v, k);\n    sort(rbegin(o), rend(o));\n    sort(rbegin(e), rend(e));        \n    if (o[0].second != e[0].second) \n      return n - o[0].first - e[0].first;    \n    int mo = o[0].first + (e.size() > 1 ? e[1].first : 0);\n    int me = e[0].first + (o.size() > 1 ? o[1].first : 0);\n    return n - max(mo, me);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array&nbsp;nums&nbsp;consisting of&nbsp;n&nbsp;positive integers. The array&nbsp;nums&nbsp;is called&nbsp;alternating&nbsp;if: nums[i &#8211; 2] == nums[i], where&nbsp;2 &lt;= i &lt;= n &#8211; 1. nums[i &#8211; 1] !=&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[712,20,88,177],"class_list":["post-9496","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-alternating","tag-array","tag-greedy","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9496","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=9496"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9496\/revisions"}],"predecessor-version":[{"id":9498,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9496\/revisions\/9498"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}