{"id":9587,"date":"2022-03-28T20:02:37","date_gmt":"2022-03-29T03:02:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9587"},"modified":"2022-03-28T20:05:01","modified_gmt":"2022-03-29T03:05:01","slug":"leetcode-2216-minimum-deletions-to-make-array-beautiful","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-2216-minimum-deletions-to-make-array-beautiful\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2216. Minimum Deletions to Make Array Beautiful"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>. The array&nbsp;<code>nums<\/code>&nbsp;is&nbsp;<strong>beautiful<\/strong>&nbsp;if:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums.length<\/code>&nbsp;is even.<\/li><li><code>nums[i] != nums[i + 1]<\/code>&nbsp;for all&nbsp;<code>i % 2 == 0<\/code>.<\/li><\/ul>\n\n\n\n<p>Note that an empty array is considered beautiful.<\/p>\n\n\n\n<p>You can delete any number of elements from&nbsp;<code>nums<\/code>. When you delete an element, all the elements to the right of the deleted element will be&nbsp;<strong>shifted one unit to the left<\/strong>&nbsp;to fill the gap created and all the elements to the left of the deleted element will remain&nbsp;<strong>unchanged<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of elements to delete from&nbsp;<\/em><code>nums<\/code><em>&nbsp;to make it&nbsp;<\/em><em>beautiful.<\/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,1,2,3,5]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> You can delete either <code>nums[0]<\/code> or <code>nums[1]<\/code> to make <code>nums<\/code> = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make <code>nums<\/code> beautiful.\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,1,2,2,3,3]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You can delete <code>nums[0]<\/code> and <code>nums[5]<\/code> to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.\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>0 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Two Pointers<\/strong><\/h2>\n\n\n\n<p>If two consecutive numbers are the same, we must remove one. We don&#8217;t need to actually remove elements from array, just need to track how many elements have been removed so far.<\/p>\n\n\n\n<p>i is the position in the original array, ans is the number of elements been removed. i &#8211; ans is the position in the updated array.<\/p>\n\n\n\n<p>ans += nums[i &#8211; ans] == nums[i &#8211; ans + 1]<\/p>\n\n\n\n<p>Remove the last element (just increase answer by 1) if the length of the new array is odd.<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minDeletion(vector<int>& nums) {\n    const int n = nums.size();\n    int ans = 0;\n    for (int i = 0; i - ans + 1 < n; i += 2)\n      ans += nums[i - ans] == nums[i - ans + 1];\n    ans += (n - ans) &#038; 1;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums. The array&nbsp;nums&nbsp;is&nbsp;beautiful&nbsp;if: nums.length&nbsp;is even. nums[i] != nums[i + 1]&nbsp;for all&nbsp;i % 2 == 0. Note that an empty array is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[177,252,175],"class_list":["post-9587","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-medium","tag-remove","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9587","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=9587"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9587\/revisions"}],"predecessor-version":[{"id":9591,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9587\/revisions\/9591"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}