<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>descending Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/descending/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/descending/</link>
	<description></description>
	<lastBuildDate>Sun, 06 Sep 2020 00:36:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>descending Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/descending/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1574. Shortest Subarray to be Removed to Make Array Sorted</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Sep 2020 00:36:24 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[descending]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7338</guid>

					<description><![CDATA[<p>Given an integer array&#160;arr, remove a&#160;subarray (can be empty) from&#160;arr&#160;such that the remaining elements in&#160;arr&#160;are&#160;non-decreasing. A subarray is a contiguous&#160;subsequence of the array. Return&#160;the length&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted/">花花酱 LeetCode 1574. Shortest Subarray to be Removed to Make Array Sorted</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>A subarray is a contiguous&nbsp;subsequence of the array.</p>



<p>Return&nbsp;<em>the length of the shortest subarray to remove</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,3,10,4,2,3,5]
<strong>Output:</strong> 3
<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.
Another correct solution is to remove the subarray [3,10,4].</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [5,4,3,2,1]
<strong>Output:</strong> 4
<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].
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,2,3]
<strong>Output:</strong> 0
<strong>Explanation: </strong>The array is already non-decreasing. We do not need to remove any elements.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1]
<strong>Output:</strong> 0
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= arr.length &lt;= 10^5</code></li><li><code>0 &lt;= arr[i] &lt;= 10^9</code></li></ul>



<h2><strong>Solution: Two Pointers</strong></h2>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int findLengthOfShortestSubarray(vector&lt;int&gt;&amp; arr) {
    const int n = arr.size();
    int j = n - 1;
    while (j &gt; 0 &amp;&amp; arr[j - 1] &lt;= arr[j]) --j;
    if (j == 0) return 0;
    int ans = j; // remove arr[0~j-1]
    for (int i = 0; i &lt; n; ++i) {
      if (i &gt; 0 &amp;&amp; arr[i - 1] &gt; arr[i]) break;
      while (j &lt; n &amp;&amp; arr[i] &gt; arr[j]) ++j;      
      // arr[i] &lt;= arr[j], remove arr[i + 1 ~ j - 1]
      ans = min(ans, j - i - 1);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted/">花花酱 LeetCode 1574. Shortest Subarray to be Removed to Make Array Sorted</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1574-shortest-subarray-to-be-removed-to-make-array-sorted/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
