{"id":7338,"date":"2020-09-05T17:36:24","date_gmt":"2020-09-06T00:36:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7338"},"modified":"2020-09-05T17:36:34","modified_gmt":"2020-09-06T00:36:34","slug":"leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1574. Shortest Subarray to be Removed to Make Array Sorted"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>arr<\/code>, remove a&nbsp;subarray (can be empty) from&nbsp;<code>arr<\/code>&nbsp;such that the remaining elements in&nbsp;<code>arr<\/code>&nbsp;are&nbsp;<strong>non-decreasing<\/strong>.<\/p>\n\n\n\n<p>A subarray is a contiguous&nbsp;subsequence of the array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the length of the shortest subarray to remove<\/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> arr = [1,2,3,10,4,2,3,5]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.\nAnother correct solution is to remove the subarray [3,10,4].<\/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> arr = [5,4,3,2,1]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].\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> arr = [1,2,3]\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>The array is already non-decreasing. We do not need to remove any elements.\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> arr = [1]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>0 &lt;= arr[i] &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Find the right most j such that arr[j &#8211; 1] &gt; arr[j], if not found which means the entire array is sorted return 0. Then we have a non-descending subarray arr[j~n-1].<br><br>We maintain two pointers i, j, such that arr[0~i] is non-descending and arr[i] &lt;= arr[j] which means we can remove arr[i+1~j-1] to get a non-descending array. Number of elements to remove is j &#8211; i &#8211; 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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findLengthOfShortestSubarray(vector<int>& arr) {\n    const int n = arr.size();\n    int j = n - 1;\n    while (j > 0 && arr[j - 1] <= arr[j]) --j;\n    if (j == 0) return 0;\n    int ans = j; \/\/ remove arr[0~j-1]\n    for (int i = 0; i < n; ++i) {\n      if (i > 0 && arr[i - 1] > arr[i]) break;\n      while (j < n &#038;&#038; arr[i] > arr[j]) ++j;      \n      \/\/ arr[i] <= arr[j], remove arr[i + 1 ~ j - 1]\n      ans = min(ans, j - i - 1);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;arr, remove a&nbsp;subarray (can be empty) from&nbsp;arr&nbsp;such that the remaining elements in&nbsp;arr&nbsp;are&nbsp;non-decreasing. A subarray is a contiguous&nbsp;subsequence of the array. Return&nbsp;the length&#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":[20,646,175],"class_list":["post-7338","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-array","tag-descending","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7338","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=7338"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7338\/revisions"}],"predecessor-version":[{"id":7340,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7338\/revisions\/7340"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}