<?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>mountain array Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mountain-array/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/mountain-array/</link>
	<description></description>
	<lastBuildDate>Sat, 28 Nov 2020 21:09: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>mountain array Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/mountain-array/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1671. Minimum Number of Removals to Make Mountain Array</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1671-minimum-number-of-removals-to-make-mountain-array/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1671-minimum-number-of-removals-to-make-mountain-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 28 Nov 2020 21:08:42 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[LIS]]></category>
		<category><![CDATA[mountain array]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7729</guid>

					<description><![CDATA[<p>You may recall that an array&#160;arr&#160;is a&#160;mountain array&#160;if and only if: arr.length &#62;= 3 There exists some index&#160;i&#160;(0-indexed) with&#160;0 &#60; i &#60; arr.length - 1&#160;such&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1671-minimum-number-of-removals-to-make-mountain-array/">花花酱 LeetCode 1671. Minimum Number of Removals to Make Mountain Array</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>You may recall that an array&nbsp;<code>arr</code>&nbsp;is a&nbsp;<strong>mountain array</strong>&nbsp;if and only if:</p>



<ul><li><code>arr.length &gt;= 3</code></li><li>There exists some index&nbsp;<code>i</code>&nbsp;(<strong>0-indexed</strong>) with&nbsp;<code>0 &lt; i &lt; arr.length - 1</code>&nbsp;such that:<ul><li><code>arr[0] &lt; arr[1] &lt; ... &lt; arr[i - 1] &lt; arr[i]</code></li><li><code>arr[i] &gt; arr[i + 1] &gt; ... &gt; arr[arr.length - 1]</code></li></ul></li></ul>



<p>Given an integer array&nbsp;<code>nums</code>​​​, return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of elements to remove to make&nbsp;</em><code>nums<em>​​​</em></code><em>a&nbsp;<strong>mountain array</strong>.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,3,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array itself is a mountain array so we do not need to remove any elements.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,1,1,5,6,2,3,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,3,2,1,1,2,3,1]
<strong>Output:</strong> 4
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,4,3,2,1]
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>3 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li><li>It is guaranteed that you can make a mountain array out of&nbsp;<code>nums</code>.</li></ul>



<h2><strong>Solution: DP / LIS</strong></h2>



<p>LIS[i] := longest increasing subsequence ends with nums[i]<br>LDS[i] := longest decreasing subsequence starts with nums[i]<br>Let nums[i] be the peak, the length of the mountain array is LIS[i] + LDS[i] &#8211; 1<br>Note: LIS[i] and LDS[i] must be &gt; 1 to form a valid mountain array.<br>ans = min(n &#8211; (LIS[i] + LDS[i] &#8211; 1)) 0 &lt;= i &lt; n</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumMountainRemovals(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; LIS(n, 1); // LIS[i] := Longest increasing subseq ends with nums[i]
    vector&lt;int&gt; LDS(n, 1); // LDS[i] := Longest decreasing subseq starts with nums[i]
    for (int i = 0; i &lt; n; ++i)      
      for (int j = 0; j &lt; i; ++j)
        if (nums[i] &gt; nums[j]) LIS[i] = max(LIS[i], LIS[j] + 1);
    for (int i = n - 1; i &gt;= 0; --i)
      for (int j = n - 1; j &gt; i; --j)
        if (nums[i] &gt; nums[j]) LDS[i] = max(LDS[i], LDS[j] + 1);
    int ans = INT_MAX;
    for (int i = 0; i &lt; n; ++i) {
      if (LIS[i] &lt; 2 || LDS[i] &lt; 2) continue;
      ans = min(ans, n - (LIS[i] + LDS[i] - 1));
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1671-minimum-number-of-removals-to-make-mountain-array/">花花酱 LeetCode 1671. Minimum Number of Removals to Make Mountain Array</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/dynamic-programming/leetcode-1671-minimum-number-of-removals-to-make-mountain-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
