<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=All_nearest_smaller_values</id>
		<title>All nearest smaller values - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=All_nearest_smaller_values"/>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;action=history"/>
		<updated>2026-05-01T10:58:42Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=2160&amp;oldid=prev</id>
		<title>172.69.33.199: /* Algorithm */ Corrected psuedocode to save index and not value of NSV of A_i</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=2160&amp;oldid=prev"/>
				<updated>2018-01-02T14:12:59Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Algorithm: &lt;/span&gt; Corrected psuedocode to save index and not value of NSV of A_i&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 14:12, 2 January 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L20&quot; &gt;Line 20:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 20:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; while A[top(S)] &amp;amp;ge; A[i]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; while A[top(S)] &amp;amp;ge; A[i]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; pop(S)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; pop(S)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; let NSV[i] &amp;amp;larr; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;A[&lt;/del&gt;top(S)&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;]&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; let NSV[i] &amp;amp;larr; top(S)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; push(S,i)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; push(S,i)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>172.69.33.199</name></author>	</entry>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=2037&amp;oldid=prev</id>
		<title>172.68.51.18: ASNV -&gt; ANSV</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=2037&amp;oldid=prev"/>
				<updated>2017-06-05T21:44:23Z</updated>
		
		<summary type="html">&lt;p&gt;ASNV -&amp;gt; ANSV&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 21:44, 5 June 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L26&quot; &gt;Line 26:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 26:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Applications==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Applications==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A theoretically important application of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ASNV &lt;/del&gt;is the construction of the [[Cartesian tree]] for a given list of numbers; a node's parent in a Cartesian tree is either its NSV or its NSV in the reversed array (that is, the nearest smaller value on the right rather than on the left), whichever one is larger. (Proof is given in the other article.)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A theoretically important application of &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;ANSV &lt;/ins&gt;is the construction of the [[Cartesian tree]] for a given list of numbers; a node's parent in a Cartesian tree is either its NSV or its NSV in the reversed array (that is, the nearest smaller value on the right rather than on the left), whichever one is larger. (Proof is given in the other article.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Running ANSV twice, once on an array and once on its reverse, can be used to find, for each element &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;, the range &amp;lt;math&amp;gt;[j,k]&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_m \geq A_i&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;m \in [j,k]&amp;lt;/math&amp;gt;. Geometrically, if the array is taken to represent a histogram in which &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is the height of the ''i''&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; bar, then this range gives how large a rectangle can be that just touches the top of the ''i''&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; bar while staying contained within the histogram. This can then trivially be used to find the largest rectangle contained entirely within the histogram.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Running ANSV twice, once on an array and once on its reverse, can be used to find, for each element &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;, the range &amp;lt;math&amp;gt;[j,k]&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_m \geq A_i&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;m \in [j,k]&amp;lt;/math&amp;gt;. Geometrically, if the array is taken to represent a histogram in which &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is the height of the ''i''&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; bar, then this range gives how large a rectangle can be that just touches the top of the ''i''&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; bar while staying contained within the histogram. This can then trivially be used to find the largest rectangle contained entirely within the histogram.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>172.68.51.18</name></author>	</entry>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=2036&amp;oldid=prev</id>
		<title>Ashutosh1598: The index i needs to be pushed onto the stack in the pseudocode.</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=2036&amp;oldid=prev"/>
				<updated>2017-05-16T19:52:53Z</updated>
		
		<summary type="html">&lt;p&gt;The index i needs to be pushed onto the stack in the pseudocode.&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 19:52, 16 May 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L21&quot; &gt;Line 21:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 21:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; pop(S)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; pop(S)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; let NSV[i] &amp;amp;larr; A[top(S)]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; let NSV[i] &amp;amp;larr; A[top(S)]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; push(S,i)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;After this code has completed, &amp;lt;code&amp;gt;NSV[i]&amp;lt;/code&amp;gt; will contain the ''index'' of the NSV of &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; (not the NSV itself).&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;After this code has completed, &amp;lt;code&amp;gt;NSV[i]&amp;lt;/code&amp;gt; will contain the ''index'' of the NSV of &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; (not the NSV itself).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Ashutosh1598</name></author>	</entry>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=1441&amp;oldid=prev</id>
		<title>Brian: whoops</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=1441&amp;oldid=prev"/>
				<updated>2011-11-21T03:07:45Z</updated>
		
		<summary type="html">&lt;p&gt;whoops&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 03:07, 21 November 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L37&quot; &gt;Line 37:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 37:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* USACO: Rectangular Barn&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* USACO: Rectangular Barn&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;{{&lt;/del&gt;Category:Pages needing code&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;}}&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;[[&lt;/ins&gt;Category:Pages needing code&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=1440&amp;oldid=prev</id>
		<title>Brian: Created page with &quot;In a list of numbers &lt;math&gt;A&lt;/math&gt;, the ''nearest smaller value'' (NSV) to an entry &lt;math&gt;A_i&lt;/math&gt; is the last entry preceding it with a smaller value, if any; that is, the en...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=All_nearest_smaller_values&amp;diff=1440&amp;oldid=prev"/>
				<updated>2011-11-21T03:07:13Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;In a list of numbers &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, the &amp;#039;&amp;#039;nearest smaller value&amp;#039;&amp;#039; (NSV) to an entry &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is the last entry preceding it with a smaller value, if any; that is, the en...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In a list of numbers &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;, the ''nearest smaller value'' (NSV) to an entry &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is the last entry preceding it with a smaller value, if any; that is, the entry &amp;lt;math&amp;gt;A_j&amp;lt;/math&amp;gt; with greatest &amp;lt;math&amp;gt;j &amp;lt; i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_j &amp;lt; A_i&amp;lt;/math&amp;gt;. The '''all nearest smaller values''' (ANSV) problem is that of computing the NSV for each number in the list.&lt;br /&gt;
&lt;br /&gt;
==Algorithm==&lt;br /&gt;
It is obvious that, upon seeing the list for the first time, we cannot guarantee being able to find the NSV to a given entry in less than linear time, because it could be any of the preceding entries in the list, and thus, in the worst case, we must examine all of them. If we do a linear scan on each element, we get a trivial &amp;lt;math&amp;gt;O(n^2)&amp;lt;/math&amp;gt; time solution to ANSV.&lt;br /&gt;
&lt;br /&gt;
However, it turns out that there is a simple linear time (&amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt;) solution to ANSV, which is clearly asymptotically optimal, since the size of the output is &amp;lt;math&amp;gt;\Theta(n)&amp;lt;/math&amp;gt; anyway. To understand this algorithm, we make a series of observations and refinements:&lt;br /&gt;
# If &amp;lt;math&amp;gt;k &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt;, then it is only possible for &amp;lt;math&amp;gt;A_k&amp;lt;/math&amp;gt; to be a NSV for &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;A_k &amp;lt; A_j&amp;lt;/math&amp;gt;; otherwise &amp;lt;math&amp;gt;A_j&amp;lt;/math&amp;gt; will always be both smaller and &amp;quot;nearer&amp;quot;.&lt;br /&gt;
# Thus, if we scan the list from left to right, and compute the NSV for each element as we visit it, and we encounter &amp;lt;math&amp;gt;A_j&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_j \leq A_k&amp;lt;/math&amp;gt; for some &amp;lt;math&amp;gt;A_k&amp;lt;/math&amp;gt; already seen, then we can pretend that &amp;lt;math&amp;gt;A_k&amp;lt;/math&amp;gt; doesn't exist anymore.&lt;br /&gt;
# Given this fact, the only entries we've seen so far that we need to keep track of are the ones that are less than all following entries seen so far.&lt;br /&gt;
# This means that the list of &amp;quot;retained&amp;quot; entries is monotonically increasing.&lt;br /&gt;
# Once we get to &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;, we can compute its NSV by looking backward though the list of retained previous entries, selecting the first one we find that is less than &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;. Also, all the retained entries skipped along the way&amp;amp;mdash;the ones that are greater than or equal to &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;&amp;amp;mdash;can now be discarded.&lt;br /&gt;
In these five points the essence of the algorithm is contained. We can use a [[stack]] to store the list of previously seen values that are being retained, because we only add to and remove from it at the end. As an implementation detail, in step 5, it is useful to have a sentinel, so that we don't try to pop off an empty stack (when &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; has no preceding smaller value).&lt;br /&gt;
&lt;br /&gt;
'''Pseudocode:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;input array A[1..n]&lt;br /&gt;
let A[0] &amp;amp;larr; -&amp;amp;infin;&lt;br /&gt;
let S be an empty stack&lt;br /&gt;
push(S,0)&lt;br /&gt;
for i &amp;amp;isin; [1..n]&lt;br /&gt;
    while A[top(S)] &amp;amp;ge; A[i]&lt;br /&gt;
        pop(S)&lt;br /&gt;
    let NSV[i] &amp;amp;larr; A[top(S)]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After this code has completed, &amp;lt;code&amp;gt;NSV[i]&amp;lt;/code&amp;gt; will contain the ''index'' of the NSV of &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; (not the NSV itself).&lt;br /&gt;
&lt;br /&gt;
==Applications==&lt;br /&gt;
A theoretically important application of ASNV is the construction of the [[Cartesian tree]] for a given list of numbers; a node's parent in a Cartesian tree is either its NSV or its NSV in the reversed array (that is, the nearest smaller value on the right rather than on the left), whichever one is larger. (Proof is given in the other article.)&lt;br /&gt;
&lt;br /&gt;
Running ANSV twice, once on an array and once on its reverse, can be used to find, for each element &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt;, the range &amp;lt;math&amp;gt;[j,k]&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;A_m \geq A_i&amp;lt;/math&amp;gt; for each &amp;lt;math&amp;gt;m \in [j,k]&amp;lt;/math&amp;gt;. Geometrically, if the array is taken to represent a histogram in which &amp;lt;math&amp;gt;A_i&amp;lt;/math&amp;gt; is the height of the ''i''&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; bar, then this range gives how large a rectangle can be that just touches the top of the ''i''&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt; bar while staying contained within the histogram. This can then trivially be used to find the largest rectangle contained entirely within the histogram.&lt;br /&gt;
&lt;br /&gt;
This gives an easy linear-time solution to the problem of finding the largest empty axis-aligned rectangle in a rectangular grid (in which each square is either completely full or completely empty): sweep from top to bottom; keep track of how far up one can go along each column  until encountering an obstacle, starting from the current row; and find the largest rectangle contained within the corresponding histogram constructed at each row.&lt;br /&gt;
&lt;br /&gt;
==Problems==&lt;br /&gt;
* {{Problem|ccc11s2p6|CCC '11 Stage 2: Biggest (Zero Carbon) Footprint}}&lt;br /&gt;
* {{SPOJ|HISTOGRA|Largest rectangle in a histogram}}&lt;br /&gt;
* {{SPOJ|CTGAME|City Game}}&lt;br /&gt;
* USACO: Rectangular Barn&lt;br /&gt;
&lt;br /&gt;
{{Category:Pages needing code}}&lt;/div&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	</feed>