<?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>two passes Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/two-passes/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/two-passes/</link>
	<description></description>
	<lastBuildDate>Sun, 25 Apr 2021 22:30:20 +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>two passes Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/two-passes/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1840. Maximum Building Height</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1840-maximum-building-height/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1840-maximum-building-height/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 25 Apr 2021 22:30:06 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[two passes]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8403</guid>

					<description><![CDATA[<p>You want to build&#160;n&#160;new buildings in a city. The new buildings will be built in a line and are labeled from&#160;1&#160;to&#160;n. However, there are city&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1840-maximum-building-height/">花花酱 LeetCode 1840. Maximum Building Height</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 want to build&nbsp;<code>n</code>&nbsp;new buildings in a city. The new buildings will be built in a line and are labeled from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>.</p>



<p>However, there are city restrictions on the heights of the new buildings:</p>



<ul><li>The height of each building must be a non-negative integer.</li><li>The height of the first building&nbsp;<strong>must</strong>&nbsp;be&nbsp;<code>0</code>.</li><li>The height difference between any two adjacent buildings&nbsp;<strong>cannot exceed</strong>&nbsp;<code>1</code>.</li></ul>



<p>Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array&nbsp;<code>restrictions</code>&nbsp;where&nbsp;<code>restrictions[i] = [id<sub>i</sub>, maxHeight<sub>i</sub>]</code>&nbsp;indicates that building&nbsp;<code>id<sub>i</sub></code>&nbsp;must have a height&nbsp;<strong>less than or equal to</strong>&nbsp;<code>maxHeight<sub>i</sub></code>.</p>



<p>It is guaranteed that each building will appear&nbsp;<strong>at most once</strong>&nbsp;in&nbsp;<code>restrictions</code>, and building&nbsp;<code>1</code>&nbsp;will&nbsp;<strong>not</strong>&nbsp;be in&nbsp;<code>restrictions</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum possible height</strong>&nbsp;of the&nbsp;<strong>tallest</strong>&nbsp;building</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/04/08/ic236-q4-ex1-1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, restrictions = [[2,1],[4,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The green area in the image indicates the maximum allowed height for each building.
We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/04/08/ic236-q4-ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, restrictions = []
<strong>Output:</strong> 5
<strong>Explanation:</strong> The green area in the image indicates the maximum allowed height for each building.
We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/04/08/ic236-q4-ex3.png" alt=""/></figure>



<pre class="crayon-plain-tag">&lt;strong&gt;Input:&lt;/strong&gt; n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
&lt;strong&gt;Output:&lt;/strong&gt; 5
&lt;strong&gt;Explanation:&lt;/strong&gt; The green area in the image indicates the maximum allowed height for each building.
We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.</pre>



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



<ul><li><code>2 &lt;= n &lt;= 10<sup>9</sup></code></li><li><code>0 &lt;= restrictions.length &lt;= min(n - 1, 10<sup>5</sup>)</code></li><li><code>2 &lt;= id<sub>i</sub>&nbsp;&lt;= n</code></li><li><code>id<sub>i</sub></code>&nbsp;is&nbsp;<strong>unique</strong>.</li><li><code>0 &lt;= maxHeight<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li></ul>



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



<p>Trim the max heights based on neighboring max heights.<br>Two passes: left to right, right to left.</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 maxBuilding(int n, vector&lt;vector&lt;int&gt;&gt;&amp; rs) {
    rs.push_back({1, 0});
    sort(begin(rs), end(rs));
    if (rs.back()[0] != n)
      rs.push_back({n, n - 1});
    
    const int m = rs.size();
    for (int i = 1; i &lt; m; ++i)
      rs[i][1] = min(rs[i][1], rs[i - 1][1] + rs[i][0] - rs[i - 1][0]);
    for (int i = m - 2; i &gt;= 0; --i)
      rs[i][1] = min(rs[i][1], rs[i + 1][1] - rs[i][0] + rs[i + 1][0]);
    
    int ans = 0;
    for (int i = 1; i &lt; m; ++i) {
      const int l = rs[i - 1][1];
      const int r = rs[i][1];
      ans = max(ans, max(l, r) + (rs[i][0] - rs[i - 1][0] - abs(l - r)) / 2);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1840-maximum-building-height/">花花酱 LeetCode 1840. Maximum Building Height</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/algorithms/array/leetcode-1840-maximum-building-height/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
