<?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>decreasing Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/decreasing/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/decreasing/</link>
	<description></description>
	<lastBuildDate>Wed, 26 Dec 2018 07:37:56 +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>decreasing Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/decreasing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 962. Maximum Width Ramp</title>
		<link>https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/</link>
					<comments>https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 26 Dec 2018 07:30:42 +0000</pubDate>
				<category><![CDATA[Stack]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[decreasing]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[stack]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4537</guid>

					<description><![CDATA[<p>Given an array&#160;A&#160;of integers, a&#160;ramp&#160;is a tuple&#160;(i, j)&#160;for which&#160;i &#60; j&#160;and&#160;A[i] &#60;= A[j].&#160; The width of such a&#160;ramp is&#160;j - i. Find the maximum width&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/">花花酱 LeetCode 962. Maximum Width Ramp</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 array&nbsp;<code>A</code>&nbsp;of integers, a&nbsp;<em>ramp</em>&nbsp;is a tuple&nbsp;<code>(i, j)</code>&nbsp;for which&nbsp;<code>i &lt; j</code>&nbsp;and&nbsp;<code>A[i] &lt;= A[j]</code>.&nbsp; The width of such a&nbsp;ramp is&nbsp;<code>j - i</code>.</p>



<p>Find the maximum width of a ramp in&nbsp;<code>A</code>.&nbsp; If one doesn&#8217;t exist, return 0.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[6,0,8,2,1,5] <br><strong>Output: </strong>4 <br><strong>Explanation: </strong> The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5. </pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[9,8,1,0,1,9,4,0,4,1] <br><strong>Output: </strong>7 <br><strong>Explanation: </strong> The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1. </pre>



<p><strong>Note:</strong></p>



<ol><li><code>2 &lt;= A.length &lt;= 50000</code></li><li><code>0 &lt;= A[i] &lt;= 50000</code></li></ol>



<h1><strong>Solution: Stack</strong></h1>



<ol><li>Using a stack to store start candidates&#8217; (decreasing order) index</li><li>Scan from right to left, compare the current number with the one on the top of the stack, pop if greater.</li></ol>



<p>e.g. <br>A = [<strong>6</strong>,<strong>0</strong>,8,2,1,5]<br>stack = [0, 1] => [6, 0] <br>cur: A[5] = 5, stack.top = A[1] = 0, ramp = 5, stack.pop()<br>cur: A[4] = 1, stack.top = A[0] = 6<br>cur: A[3] = 2, stack.top = A[0] = 6<br>cur: A[2] = 8,  stack.top = A[0] = 6, ramp = 2, stack.pop()<br>stack.isEmpty() => END</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 44 ms
class Solution {
public:
  int maxWidthRamp(vector&lt;int&gt;&amp; A) {
    stack&lt;int&gt; s;
    for (int i = 0; i &lt; A.size(); ++i)
      if (s.empty() || A[i] &lt; A[s.top()])
        s.push(i);
    int ans = 0;
    for (int i = A.size() - 1; i &gt;= 0; --i)
      while (!s.empty() &amp;&amp; A[i] &gt;= A[s.top()]) {
        ans = max(ans, i - s.top());
        s.pop();
      }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua, running time: 92 ms
class Solution:
  def maxWidthRamp(self, A):
    s = []
    for i in range(len(A)):
      if not s or A[i] &lt; A[s[-1]]: s.append(i)
    ans = 0
    for i in range(len(A) - 1, -1, -1):
      while s and A[i] &gt;= A[s[-1]]:
        ans = max(ans, i - s.pop())        
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/">花花酱 LeetCode 962. Maximum Width Ramp</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/stack/leetcode-962-maximum-width-ramp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
