{"id":8856,"date":"2021-11-28T09:02:02","date_gmt":"2021-11-28T17:02:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8856"},"modified":"2021-11-28T09:02:31","modified_gmt":"2021-11-28T17:02:31","slug":"leetcode-2091-removing-minimum-and-maximum-from-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2091-removing-minimum-and-maximum-from-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2091. Removing Minimum and Maximum From Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array of&nbsp;<strong>distinct<\/strong>&nbsp;integers&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>There is an element in&nbsp;<code>nums<\/code>&nbsp;that has the&nbsp;<strong>lowest<\/strong>&nbsp;value and an element that has the&nbsp;<strong>highest<\/strong>&nbsp;value. We call them the&nbsp;<strong>minimum<\/strong>&nbsp;and&nbsp;<strong>maximum<\/strong>&nbsp;respectively. Your goal is to remove&nbsp;<strong>both<\/strong>&nbsp;these elements from the array.<\/p>\n\n\n\n<p>A&nbsp;<strong>deletion<\/strong>&nbsp;is defined as either removing an element from the&nbsp;<strong>front<\/strong>&nbsp;of the array or removing an element from the&nbsp;<strong>back<\/strong>&nbsp;of the array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of deletions it would take to remove&nbsp;<strong>both<\/strong>&nbsp;the minimum and maximum element from the array.<\/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 = [2,<strong>10<\/strong>,7,5,4,<strong>1<\/strong>,8,6]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> \nThe minimum element in the array is nums[5], which is 1.\nThe maximum element in the array is nums[1], which is 10.\nWe can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.\nThis results in 2 + 3 = 5 deletions, which is the minimum number possible.\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 = [0,<strong>-4<\/strong>,<strong>19<\/strong>,1,8,-2,-3,5]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \nThe minimum element in the array is nums[1], which is -4.\nThe maximum element in the array is nums[2], which is 19.\nWe can remove both the minimum and maximum by removing 3 elements from the front.\nThis results in only 3 deletions, which is the minimum number possible.\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 = [<strong>101<\/strong>]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>  \nThere is only one element in the array, which makes it both the minimum and maximum element.\nWe can remove it with 1 deletion.\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>-10<sup>5<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li>The integers in&nbsp;<code>nums<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Three ways<\/strong><\/h2>\n\n\n\n<p>There are only three ways to remove min\/max elements.<br>1) Remove front elements<br>2) Remove back elements<br>3) Remove one with front elements, and another one with back elements.<\/p>\n\n\n\n<p>Just find the best way to do it.<\/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 minimumDeletions(vector<int>& nums) {\n    const int n = nums.size();\n    auto [it1, it2] = minmax_element(begin(nums), end(nums));\n    int i = it1 - begin(nums);\n    int j = it2 - begin(nums);\n    return min({max(i, j) + 1, \/\/ remove front elements\n                n - min(i, j), \/\/ remove back elements\n                min(i, j) + 1 + n - max(i, j)} \/\/ front + back\n              );    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array of&nbsp;distinct&nbsp;integers&nbsp;nums. There is an element in&nbsp;nums&nbsp;that has the&nbsp;lowest&nbsp;value and an element that has the&nbsp;highest&nbsp;value. We call them the&nbsp;minimum&nbsp;and&nbsp;maximum&nbsp;respectively. Your goal 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":[184],"tags":[20,177],"class_list":["post-8856","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8856","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=8856"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8856\/revisions"}],"predecessor-version":[{"id":8858,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8856\/revisions\/8858"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}