<?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>Heap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/heap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/heap/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2023 05:15:09 +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>Heap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/heap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2558. Take Gifts From the Richest Pile</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Feb 2023 05:12:41 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[max heap]]></category>
		<category><![CDATA[priority queue]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9923</guid>

					<description><![CDATA[<p>You are given an integer array&#160;gifts&#160;denoting the number of gifts in various piles. Every second, you do the following: Choose the pile with the maximum&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/">花花酱 LeetCode 2558. Take Gifts From the Richest Pile</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 are given an integer array&nbsp;<code>gifts</code>&nbsp;denoting the number of gifts in various piles. Every second, you do the following:</p>



<ul><li>Choose the pile with the maximum number of gifts.</li><li>If there is more than one pile with the maximum number of gifts, choose any.</li><li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li></ul>



<p>Return&nbsp;<em>the number of gifts remaining after&nbsp;</em><code>k</code><em>&nbsp;seconds.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
<strong>Output:</strong> 29
<strong>Explanation:</strong> 
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> gifts = [1,1,1,1], k = 4
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. 
That is, you can't take any pile with you. 
So, the total gifts remaining are 4.
</pre>



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



<ul><li><code>1 &lt;= gifts.length &lt;= 10<sup>3</sup></code></li><li><code>1 &lt;= gifts[i] &lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= k &lt;= 10<sup>3</sup></code></li></ul>



<h2><strong>Solution: Priority Queue</strong></h2>



<p>Keep all numbers in a priority queue (max heap), each time extract the top one (largest one), then put num  &#8211; sqrt(num) back to the queue.</p>



<p>Tip: We can early return if all the numbers become 1.</p>



<p>Time complexity: O(n + klogn)<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:
  long long pickGifts(vector&lt;int&gt;&amp; gifts, int k) {
    long long ans = accumulate(begin(gifts), end(gifts), 0LL);
    priority_queue&lt;int&gt; q(begin(gifts), end(gifts));
    while (k-- &amp;&amp; q.top() &gt; 1) {
      int cur = q.top(); q.pop();
      int next = sqrt(cur);
      ans -= (cur - next);
      q.push(next);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2558-take-gifts-from-the-richest-pile/">花花酱 LeetCode 2558. Take Gifts From the Richest Pile</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/heap/leetcode-2558-take-gifts-from-the-richest-pile/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2208. Minimum Operations to Halve Array Sum</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-2208-minimum-operations-to-halve-array-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-2208-minimum-operations-to-halve-array-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 21 Mar 2022 12:21:32 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(nlogn)]]></category>
		<category><![CDATA[priority_queue]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9580</guid>

					<description><![CDATA[<p>You are given an array&#160;nums&#160;of positive integers. In one operation, you can choose&#160;any&#160;number from&#160;nums&#160;and reduce it to&#160;exactly&#160;half the number. (Note that you may choose this&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2208-minimum-operations-to-halve-array-sum/">花花酱 LeetCode 2208. Minimum Operations to Halve Array Sum</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 are given an array&nbsp;<code>nums</code>&nbsp;of positive integers. In one operation, you can choose&nbsp;<strong>any</strong>&nbsp;number from&nbsp;<code>nums</code>&nbsp;and reduce it to&nbsp;<strong>exactly</strong>&nbsp;half the number. (Note that you may choose this reduced number in future operations.)</p>



<p>Return<em>&nbsp;the&nbsp;<strong>minimum</strong>&nbsp;number of operations to reduce the sum of&nbsp;</em><code>nums</code><em>&nbsp;by&nbsp;<strong>at least</strong>&nbsp;half.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,19,8,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33.
The following is one of the ways to reduce the sum by at least half:
Pick the number 19 and reduce it to 9.5.
Pick the number 9.5 and reduce it to 4.75.
Pick the number 8 and reduce it to 4.
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75. 
The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 &gt;= 33/2 = 16.5.
Overall, 3 operations were used so we return 3.
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,8,20]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The initial sum of nums is equal to 3 + 8 + 20 = 31.
The following is one of the ways to reduce the sum by at least half:
Pick the number 20 and reduce it to 10.
Pick the number 10 and reduce it to 5.
Pick the number 3 and reduce it to 1.5.
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5. 
The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 &gt;= 31/2 = 16.5.
Overall, 3 operations were used so we return 3.
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>7</sup></code></li></ul>



<h2><strong>Solution: Greedy + PriorityQueue/Max Heap</strong></h2>



<p>Always half the largest number, put all the numbers onto a max heap (priority queue), extract the largest one, and put reduced number back.</p>



<p>Time complexity: O(nlogn)<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 halveArray(vector&lt;int&gt;&amp; nums) {
    double sum = accumulate(begin(nums), end(nums), 0.0);
    priority_queue&lt;double&gt; q;
    for (int num : nums) q.push(num);
    int ans = 0;
    for (double cur = sum; cur &gt; sum / 2; ++ans) {    
      double num = q.top(); q.pop();
      cur -= num / 2;
      q.push(num / 2);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2208-minimum-operations-to-halve-array-sum/">花花酱 LeetCode 2208. Minimum Operations to Halve Array Sum</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/heap/leetcode-2208-minimum-operations-to-halve-array-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2054. Two Best Non-Overlapping Events</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Nov 2021 02:16:37 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8653</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;2D integer array of&#160;events&#160;where&#160;events[i] = [startTimei, endTimei, valuei]. The&#160;ith&#160;event starts at&#160;startTimeiand ends at&#160;endTimei, and if you attend this event, you will receive&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/">花花酱 LeetCode 2054. Two Best Non-Overlapping Events</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 are given a&nbsp;<strong>0-indexed</strong>&nbsp;2D integer array of&nbsp;<code>events</code>&nbsp;where&nbsp;<code>events[i] = [startTime<sub>i</sub>, endTime<sub>i</sub>, value<sub>i</sub>]</code>. The&nbsp;<code>i<sup>th</sup></code>&nbsp;event starts at&nbsp;<code>startTime<sub>i</sub></code>and ends at&nbsp;<code>endTime<sub>i</sub></code>, and if you attend this event, you will receive a value of&nbsp;<code>value<sub>i</sub></code>. You can choose&nbsp;<strong>at most</strong>&nbsp;<strong>two</strong>&nbsp;<strong>non-overlapping</strong>&nbsp;events to attend such that the sum of their values is&nbsp;<strong>maximized</strong>.</p>



<p>Return&nbsp;<em>this&nbsp;<strong>maximum</strong>&nbsp;sum.</em></p>



<p>Note that the start time and end time is&nbsp;<strong>inclusive</strong>: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time&nbsp;<code>t</code>, the next event must start at or after&nbsp;<code>t + 1</code>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/21/picture5.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,3,2],[4,5,2],[2,4,3]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/21/picture1.png" alt="Example 1 Diagram"/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,3,2],[4,5,2],[1,5,5]]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Choose event 2 for a sum of 5.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/09/21/picture3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> events = [[1,5,3],[1,5,1],[6,6,5]]
<strong>Output:</strong> 8
<strong>Explanation: </strong>Choose events 0 and 2 for a sum of 3 + 5 = 8.</pre>



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



<ul><li><code>2 &lt;= events.length &lt;= 10<sup>5</sup></code></li><li><code>events[i].length == 3</code></li><li><code>1 &lt;= startTime<sub>i</sub>&nbsp;&lt;= endTime<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= value<sub>i</sub>&nbsp;&lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: Sort + Heap</strong></h2>



<p>Sort events by start time, process them from left to right.</p>



<p>Use a min heap to store the events processed so far, a variable cur to track the max value of a non-overlapping event.</p>



<p>For a given event, pop all non-overlapping events whose end time is smaller than its start time and update cur.</p>



<p>ans = max(val + cur)</p>



<p>Time complexity: O(nlogn)<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 maxTwoEvents(vector&lt;vector&lt;int&gt;&gt;&amp; events) {
    using E = pair&lt;int,int&gt;;
    sort(begin(events), end(events));
    priority_queue&lt;E, vector&lt;E&gt;, greater&lt;E&gt;&gt; q; // (end_time, val)
    int ans = 0;
    int cur = 0;
    for (const auto&amp; e : events) {
      while (!q.empty() &amp;&amp; q.top().first &lt; e[0]) {
        cur = max(cur, q.top().second);
        q.pop();
      }
      ans = max(ans, cur + e[2]);
      q.emplace(e[1], e[2]);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-2054-two-best-non-overlapping-events/">花花酱 LeetCode 2054. Two Best Non-Overlapping Events</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/heap/leetcode-2054-two-best-non-overlapping-events/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1642. Furthest Building You Can Reach</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-1642-furthest-building-you-can-reach/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-1642-furthest-building-you-can-reach/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Nov 2020 17:36:27 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7598</guid>

					<description><![CDATA[<p>You are given an integer array&#160;heights&#160;representing the heights of buildings, some&#160;bricks, and some&#160;ladders. You start your journey from building&#160;0&#160;and move to the next building by&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-1642-furthest-building-you-can-reach/">花花酱 LeetCode 1642. Furthest Building You Can Reach</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1642. Furthest Building You Can Reach - 刷题找工作 EP366" width="500" height="281" src="https://www.youtube.com/embed/FowBaF5hYcY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an integer array&nbsp;<code>heights</code>&nbsp;representing the heights of buildings, some&nbsp;<code>bricks</code>, and some&nbsp;<code>ladders</code>.</p>



<p>You start your journey from building&nbsp;<code>0</code>&nbsp;and move to the next building by possibly using bricks or ladders.</p>



<p>While moving from building&nbsp;<code>i</code>&nbsp;to building&nbsp;<code>i+1</code>&nbsp;(<strong>0-indexed</strong>),</p>



<ul><li>If the current building&#8217;s height is&nbsp;<strong>greater than or equal</strong>&nbsp;to the next building&#8217;s height, you do&nbsp;<strong>not</strong>&nbsp;need a ladder or bricks.</li><li>If the current building&#8217;s height is&nbsp;<strong>less than</strong>&nbsp;the next building&#8217;s height, you can either use&nbsp;<strong>one ladder</strong>&nbsp;or&nbsp;<code>(h[i+1] - h[i])</code>&nbsp;<strong>bricks</strong>.</li></ul>



<p><em>Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/10/27/q4.gif" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 &gt;= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 &lt; 7.
- Go to building 3 without using ladders nor bricks since 7 &gt;= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 &lt; 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
<strong>Output:</strong> 7
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> heights = [14,3,19,3], bricks = 17, ladders = 0
<strong>Output:</strong> 3
</pre>



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



<ul><li><code>1 &lt;= heights.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= heights[i] &lt;= 10<sup>6</sup></code></li><li><code>0 &lt;= bricks &lt;= 10<sup>9</sup></code></li><li><code>0 &lt;= ladders &lt;= heights.length</code></li></ul>



<h2><strong>Solution 0: DFS</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-1.png" alt="" class="wp-image-7602" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



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



<p>AC but should be TLE</p>



<h2><strong>Solution 1: Binary Search + Greedy</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-2.png" alt="" class="wp-image-7601" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Guess we can reach to m, sort the height differences from 0~m. Use ladders for larger values and use bricks for smallest values left.</p>



<p>Time complexity: O(nlogn)<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, 300 ms
class Solution {
public:
  int furthestBuilding(vector&lt;int&gt;&amp; heights, int bricks, int ladders) {
    const int n = heights.size();
    if (ladders &gt;= n - 1) return n - 1;
    vector&lt;int&gt;  diffs(n);
    for (int i = 1; i &lt; n; ++i)
      diffs[i - 1] = max(0, heights[i] - heights[i - 1]);
    int l = ladders;
    int r = n;
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      vector&lt;int&gt; d(begin(diffs), begin(diffs) + m);
      nth_element(begin(d), end(d) - ladders, end(d));
      if (accumulate(begin(d), end(d) - ladders, 0) &gt; bricks)
        r = m;
      else
        l = m + 1;
    }
    return l - 1;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Min heap</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-3.png" alt="" class="wp-image-7603" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1642-ep366-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Use a min heap to store all the height differences ( &gt; 0) so far, if heap size is greater than ladders, which means we have to use bricks, extract the smallest value and subtract the bricks.</p>



<p>Time complexity: O(nlogk)<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, 216 ms
class Solution {
public:
  int furthestBuilding(vector&lt;int&gt;&amp; heights, int bricks, int ladders) {
    const int n = heights.size();        
    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; q;
    for (int i = 1; i &lt; n; ++i) {
      const int d = heights[i] - heights[i - 1];
      if (d &lt;= 0) continue;
      q.push(d);
      if (q.size() &lt;= ladders) continue;
      bricks -= q.top(); q.pop();
      if (bricks &lt; 0) return i - 1;      
    }
    return n - 1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-1642-furthest-building-you-can-reach/">花花酱 LeetCode 1642. Furthest Building You Can Reach</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/heap/leetcode-1642-furthest-building-you-can-reach/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1094. Car Pooling</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-1094-car-pooling/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-1094-car-pooling/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Jun 2019 04:32:35 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5241</guid>

					<description><![CDATA[<p>You are driving a vehicle that&#160;has&#160;capacity&#160;empty seats initially available for passengers.&#160; The vehicle&#160;only&#160;drives east (ie. it&#160;cannot&#160;turn around and drive west.) Given a list of&#160;trips,&#160;trip[i] =&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-1094-car-pooling/">花花酱 LeetCode 1094. Car Pooling</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 are driving a vehicle that&nbsp;has&nbsp;<code>capacity</code>&nbsp;empty seats initially available for passengers.&nbsp; The vehicle&nbsp;<strong>only</strong>&nbsp;drives east (ie. it&nbsp;<strong>cannot</strong>&nbsp;turn around and drive west.)</p>



<p>Given a list of&nbsp;<code>trips</code>,&nbsp;<code>trip[i] = [num_passengers, start_location, end_location]</code>&nbsp;contains information about the&nbsp;<code>i</code>-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off.&nbsp; The locations are given as the number of kilometers&nbsp;due east from your vehicle&#8217;s initial location.</p>



<p>Return&nbsp;<code>true</code>&nbsp;if and only if&nbsp;it is possible to pick up and drop off all passengers for all the given trips.&nbsp;</p>



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



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong>trips = [[2,1,5],[3,3,7]], capacity = 5
<strong>Output: </strong>true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong>trips = [[2,1,5],[3,5,7]], capacity = 3
<strong>Output: </strong>true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong>trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
<strong>Output: </strong>true</pre>



<h2><strong>Solution1: Min heap</strong></h2>



<p>Sort events by location</p>



<p>Time complexity: O(nlogn)<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:
  bool carPooling(vector&lt;vector&lt;int&gt;&gt;&amp; trips, int capacity) {
    priority_queue&lt;int&gt; q;
    for (const auto&amp; trip : trips) {
      int pick_up = -((trip[1] &lt;&lt; 10) | (1 &lt;&lt; 9) | trip[0]);
      int drop_off = -((trip[2] &lt;&lt; 10) | trip[0]);
      q.push(pick_up);
      q.push(drop_off);
    }
    while (q.size()) {
      int key = -q.top(); q.pop();
      int sign = ((key &gt;&gt; 9) &amp; 1) ? 1: -1;
      int num = key &amp; 0xFF;
      if ((capacity -= sign * num) &lt; 0)
        return false;
    }
    return true;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Preprocessing</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool carPooling(vector&lt;vector&lt;int&gt;&gt;&amp; trips, int capacity) {
    vector&lt;int&gt; d(1001);
    for (const auto&amp; trip : trips) {
      d[trip[1]] -= trip[0];
      d[trip[2]] += trip[0];
    }
    for (const int c : d) 
      if ((capacity += c) &lt; 0) return false;
    return true;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-1094-car-pooling/">花花酱 LeetCode 1094. Car Pooling</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/heap/leetcode-1094-car-pooling/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 778. Swim in Rising Water</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-778-swim-in-rising-water/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-778-swim-in-rising-water/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 01 Feb 2019 04:46:57 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[priority queue]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4750</guid>

					<description><![CDATA[<p>On an N x N&#160;grid, each square&#160;grid[i][j]&#160;represents the elevation at that point&#160;(i,j). Now rain starts to fall. At time&#160;t, the depth of the water everywhere&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-778-swim-in-rising-water/">花花酱 LeetCode 778. Swim in Rising Water</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/umdk98ynLSY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>On an N x N&nbsp;<code>grid</code>, each square&nbsp;<code>grid[i][j]</code>&nbsp;represents the elevation at that point&nbsp;<code>(i,j)</code>.</p>



<p>Now rain starts to fall. At time&nbsp;<code>t</code>, the depth of the water everywhere is&nbsp;<code>t</code>. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are&nbsp;at most&nbsp;<code>t</code>. You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.</p>



<p>You start at the top left square&nbsp;<code>(0, 0)</code>. What is the least time until you can reach the bottom right square&nbsp;<code>(N-1, N-1)</code>?</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input:</strong> [[0,2],[1,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
At time <code>0</code>, you are in grid location <code>(0, 0)</code>.
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.

You cannot reach point <code>(1, 1)</code> until time <code>3</code>.
When the depth of water is <code>3</code>, we can swim anywhere inside the grid.
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input:</strong> [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
<strong>Output:</strong> 16
<strong>Explanation:</strong>
<strong> 0  1  2  3  4</strong>
24 23 22 21  <strong>5</strong>
<strong>12 13 14 15 16</strong>
<strong>11</strong> 17 18 19 20
<strong>10  9  8  7  6</strong>

The final route is marked in bold.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
</pre>



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



<ol><li><code>2 &lt;= N &lt;= 50</code>.</li><li>grid[i][j] is a permutation of [0, &#8230;, N*N &#8211; 1].</li></ol>



<h2><strong>Solution 1: Dijkstra&#8217;s Algorithm</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 8 ms
class Solution {
public:
  int swimInWater(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    const int n = grid.size();
    priority_queue&lt;pair&lt;int, int&gt;&gt; q; // {-time, y * N + x}
    q.push({-grid[0][0], 0 * n + 0});
    vector&lt;int&gt; seen(n * n);
    vector&lt;int&gt; dirs{-1, 0, 1, 0, -1};
    seen[0 * n + 0] = 1;
    while (!q.empty()) {
      auto node = q.top(); q.pop();
      int t = -node.first;
      int x = node.second % n;
      int y = node.second / n;      
      if (x == n - 1 &amp;&amp; y == n - 1) return t;
      for (int i = 0; i &lt; 4; ++i) {
        int tx = x + dirs[i];
        int ty = y + dirs[i + 1];
        if (tx &lt; 0 || ty &lt; 0 || tx &gt;= n || ty &gt;= n) continue;
        if (seen[ty * n + tx]) continue;
        seen[ty * n + tx] = 1;
        q.push({-max(t, grid[ty][tx]), ty * n + tx});
      }
    }
    return -1;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Binary Search + BFS</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 8 ms
class Solution {
public:
  int swimInWater(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    const int n = grid.size();
    auto hasPath = [&amp;grid, n](int t) {
      if (grid[0][0] &gt; t) return false;      
      queue&lt;int&gt; q;
      vector&lt;int&gt; seen(n * n);
      vector&lt;int&gt; dirs{1, 0, -1, 0, 1};
      q.push(0);
      
      while (!q.empty()) {
        const int x = q.front() % n;
        const int y = q.front() / n;
        q.pop();
        if (x == n - 1 &amp;&amp; y == n - 1) return true;
        for (int i = 0; i &lt; 4; ++i) {
          const int tx = x + dirs[i];
          const int ty = y + dirs[i + 1];
          if (tx &lt; 0 || ty &lt; 0 || tx &gt;= n || ty &gt;= n || grid[ty][tx] &gt; t) continue;
          const int key = ty * n + tx;
          if (seen[key]) continue;
          seen[key] = 1;
          q.push(key);
        }
      }
      return false;
    };
    int l = 0;
    int r = n * n;
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      if (hasPath(m)) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    return l;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-778-swim-in-rising-water/">花花酱 LeetCode 778. Swim in Rising Water</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/heap/leetcode-778-swim-in-rising-water/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 703. Kth Largest Element in a Stream</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 13 Jul 2018 03:33:59 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[top k]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3095</guid>

					<description><![CDATA[<p>Problem Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/">花花酱 LeetCode 703. Kth Largest Element in a Stream</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Design a class to find the <strong>k</strong>th largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.</p>
<p>Your <code>KthLargest</code> class will have a constructor which accepts an integer <code>k</code> and an integer array <code>nums</code>, which contains initial elements from the stream. For each call to the method <code>KthLargest.add</code>, return the element representing the kth largest element in the stream.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false">int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3);   // returns 4
kthLargest.add(5);   // returns 5
kthLargest.add(10);  // returns 5
kthLargest.add(9);   // returns 8
kthLargest.add(4);   // returns 8
</pre>
<p><strong>Note: </strong><br />
You may assume that <code>nums</code>&#8216; length ≥ <code>k-1</code> and <code>k</code> ≥ 1.</p>
<h1><strong>Solution: BST / Min Heap</strong></h1>
<p>Time complexity: O(nlogk)</p>
<p>Space complexity: O(k)</p>
<p>C++ / BST</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms
class KthLargest {
public:
  KthLargest(int k, vector&lt;int&gt; nums): k_(k) {
    for (int num : nums)
      add(num);
  }

  int add(int val) {    
    s_.insert(val);
    if (s_.size() &gt; k_)
      s_.erase(s_.begin());
    return *s_.begin();
  }
private:
  const int k_;  
  multiset&lt;int&gt; s_;
};</pre><p>C++ / Min Heap</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 28 ms
class KthLargest {
public:
  KthLargest(int k, vector&lt;int&gt; nums): k_(k) {    
    for (int num : nums)
      add(num);
  }

  int add(int val) {    
    s_.push(val);
    if (s_.size() &gt; k_)
      s_.pop();
    return s_.top();
  }
private:
  const int k_;  
  priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; s_; // min heap
};</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-703-kth-largest-element-in-a-stream/">花花酱 LeetCode 703. Kth Largest Element in a Stream</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/heap/leetcode-703-kth-largest-element-in-a-stream/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 239. Sliding Window Maximum</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-239-sliding-window-maximum/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-239-sliding-window-maximum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 25 Jan 2018 05:17:37 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Heap]]></category>
		<category><![CDATA[deque]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[queue]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1650</guid>

					<description><![CDATA[<p>题目大意：给你一个数组，让你输出移动窗口的最大值。 Problem: https://leetcode.com/problems/sliding-window-maximum/ Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-239-sliding-window-maximum/">花花酱 LeetCode 239. Sliding Window Maximum</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><iframe width="500" height="375" src="https://www.youtube.com/embed/2SXqBsTR6a8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：给你一个数组，让你输出移动窗口的最大值。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/sliding-window-maximum/">https://leetcode.com/problems/sliding-window-maximum/</a></p>
<p>Given an array <i>nums</i>, there is a sliding window of size <i>k</i> which is moving from the very left of the array to the very right. You can only see the <i>k</i> numbers in the window. Each time the sliding window moves right by one position.</p>
<p>For example,<br />
Given <i>nums</i> = <code>[1,3,-1,-3,5,3,6,7]</code>, and <i>k</i> = 3.</p><pre class="crayon-plain-tag">Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7</pre><p>Therefore, return the max sliding window as <code>[3,3,5,5,6,7]</code>.</p>
<p><b>Note: </b><br />
You may assume <i>k</i> is always valid, ie: 1 ≤ k ≤ input array&#8217;s size for non-empty array.</p>
<p><b>Follow up:</b><br />
Could you solve it in linear time?</p>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<p><strong>Idea:</strong></p>
<p><img class="alignnone size-full wp-image-1657" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-1656" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/239-ep159-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>&nbsp;</p>
<h1><strong>Solution 1: Brute Force</strong></h1>
<p>Time complexity: O((n &#8211; k + 1) * k)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 180 ms
class Solution {
public:
  vector&lt;int&gt; maxSlidingWindow(vector&lt;int&gt;&amp; nums, int k) {    
    vector&lt;int&gt; ans;
    for (int i = k - 1; i &lt; nums.size(); ++i) {
      ans.push_back(*max_element(nums.begin() + i - k + 1, nums.begin() + i + 1));
    }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 80 ms
class Solution {
  public int[] maxSlidingWindow(int[] nums, int k) {
    if (k == 0) return new int[0];
    
    int[] ans = new int[nums.length - k + 1];    
    for (int i = k - 1; i &lt; nums.length; ++i) {
      int maxNum = nums[i];
      for (int j = 1; j &lt; k; ++j)
        if (nums[i - j] &gt; maxNum) maxNum = nums[i - j];
      ans[i - k + 1] = maxNum;
    }
    return ans;
  }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 1044 ms
"""
class Solution:
  def maxSlidingWindow(self, nums, k):
    if not nums: return []    
    return [max(nums[i:i+k]) for i in range(len(nums) - k + 1)]</pre><p></div></div></p>
<h1><strong>Solution 2: BST</strong></h1>
<p>Time complexity: O((n &#8211; k + 1) * logk)</p>
<p>Space complexity: O(k)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 82 ms
class Solution {
public:
  vector&lt;int&gt; maxSlidingWindow(vector&lt;int&gt;&amp; nums, int k) {
    vector&lt;int&gt; ans;
    if (nums.empty()) return ans;
    multiset&lt;int&gt; window(nums.begin(), nums.begin() + k - 1);
    for (int i = k - 1; i &lt; nums.size(); ++i) {
      window.insert(nums[i]);
      ans.push_back(*window.rbegin());
      if (i - k + 1 &gt;= 0)
        window.erase(window.equal_range(nums[i - k + 1]).first);      
    }
    return ans;
  }
};</pre><p></div></div></p>
<h1><strong>Solution 3: Monotonic Queue</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(k)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 70 ms
class MonotonicQueue {
public:
  void push(int e) {
    while(!data_.empty() &amp;&amp; e &gt; data_.back()) data_.pop_back();
    data_.push_back(e);
  } 
  
  void pop() {
    data_.pop_front();
  }
  
  int max() const { return data_.front(); }
private:
  deque&lt;int&gt; data_;
};

class Solution {
public:
  vector&lt;int&gt; maxSlidingWindow(vector&lt;int&gt;&amp; nums, int k) {
    MonotonicQueue q;
    vector&lt;int&gt; ans;
        
    for (int i = 0; i &lt; nums.size(); ++i) {
      q.push(nums[i]);
      if (i - k + 1 &gt;= 0) {
        ans.push_back(q.max());
        if (nums[i - k + 1] == q.max()) q.pop();
      }      
    }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">C++ V2</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 65 ms
class Solution {
public:
  vector&lt;int&gt; maxSlidingWindow(vector&lt;int&gt;&amp; nums, int k) {
    deque&lt;int&gt; index;
    vector&lt;int&gt; ans;
        
    for (int i = 0; i &lt; nums.size(); ++i) {
      while (!index.empty() &amp;&amp; nums[i] &gt;= nums[index.back()]) index.pop_back();
      index.push_back(i);      
      if (i - k + 1 &gt;= 0) ans.push_back(nums[index.front()]);
      if (i - k + 1 &gt;= index.front()) index.pop_front();
    }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">C++ V3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 67 ms
class Solution {
public:
  vector&lt;int&gt; maxSlidingWindow(vector&lt;int&gt;&amp; nums, int k) {
    deque&lt;int&gt; q;
    vector&lt;int&gt; ans;
        
    for (int i = 0; i &lt; nums.size(); ++i) {
      while (!q.empty() &amp;&amp; nums[i] &gt; q.back()) q.pop_back();
      q.push_back(nums[i]);
      const int s = i - k + 1;
      if (s &lt; 0) continue;      
      ans.push_back(q.front());
      if (nums[s] == q.front()) q.pop_front();
    }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 19 ms
class Solution {
  public int[] maxSlidingWindow(int[] nums, int k) {
    if (k == 0) return nums;
    
    int[] ans = new int[nums.length - k + 1];
    Deque&lt;Integer&gt; indices = new LinkedList&lt;&gt;();
    
    for (int i = 0; i &lt; nums.length; ++i) {
      while (indices.size() &gt; 0 &amp;&amp; nums[i] &gt;= nums[indices.getLast()])
        indices.removeLast();
      
      indices.addLast(i);
      if (i - k + 1 &gt;= 0) ans[i - k + 1] = nums[indices.getFirst()];
      if (i - k + 1 &gt;= indices.getFirst()) indices.removeFirst();
    }
             
    return ans;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 238 ms
"""
class MaxQueue:
  def __init__(self):
    self.q_ = collections.deque()
  
  def push(self, e):
    while self.q_ and e &gt; self.q_[-1]: self.q_.pop()
    self.q_.append(e)
  
  def pop(self):
    self.q_.popleft()
  
  def max(self):
    return self.q_[0]
    
class Solution:
  def maxSlidingWindow(self, nums, k):
    q = MaxQueue()
    ans = []
    for i in range(len(nums)):
      q.push(nums[i])
      if i &gt;= k - 1: 
        ans.append(q.max())
        if nums[i - k + 1] == q.max(): q.pop()
    return ans</pre><p></div><h2 class="tabtitle">Python3 V2</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 193 ms
"""
class Solution:
  def maxSlidingWindow(self, nums, k):
    indices = collections.deque()
    ans = []
    for i in range(len(nums)):
      while indices and nums[i] &gt;= nums[indices[-1]]: indices.pop()
      indices.append(i)
      if i &gt;= k - 1: ans.append(nums[indices[0]])
      if i - k + 1 == indices[0]: indices.popleft()
    return ans</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-239-sliding-window-maximum/">花花酱 LeetCode 239. Sliding Window Maximum</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/heap/leetcode-239-sliding-window-maximum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 692. Top K Frequent Words</title>
		<link>https://zxi.mytechroad.com/blog/heap/leetcode-692-top-k-frequent-words/</link>
					<comments>https://zxi.mytechroad.com/blog/heap/leetcode-692-top-k-frequent-words/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Oct 2017 00:51:38 +0000</pubDate>
				<category><![CDATA[Heap]]></category>
		<category><![CDATA[Medium]]></category>
		<category><![CDATA[frequency]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[priority queue]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=661</guid>

					<description><![CDATA[<p>Problem: Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-692-top-k-frequent-words/">花花酱 LeetCode 692. Top K Frequent Words</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><iframe width="500" height="375" src="https://www.youtube.com/embed/POERw4yDVBw?feature=oembed" frameborder="0" gesture="media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given a non-empty list of words, return the <i>k</i> most frequent elements.</p>
<p>Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [&quot;i&quot;, &quot;love&quot;, &quot;leetcode&quot;, &quot;i&quot;, &quot;love&quot;, &quot;coding&quot;], k = 2
Output: [&quot;i&quot;, &quot;love&quot;]
Explanation: &quot;i&quot; and &quot;love&quot; are the two most frequent words.
    Note that &quot;i&quot; comes before &quot;love&quot; due to a lower alphabetical order.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: [&quot;the&quot;, &quot;day&quot;, &quot;is&quot;, &quot;sunny&quot;, &quot;the&quot;, &quot;the&quot;, &quot;the&quot;, &quot;sunny&quot;, &quot;is&quot;, &quot;is&quot;], k = 4
Output: [&quot;the&quot;, &quot;is&quot;, &quot;sunny&quot;, &quot;day&quot;]
Explanation: &quot;the&quot;, &quot;is&quot;, &quot;sunny&quot; and &quot;day&quot; are the four most frequent words,
    with the number of occurrence being 4, 3, 2 and 1 respectively.</pre><p><b>Note:</b></p>
<ol>
<li>You may assume <i>k</i> is always valid, 1 ≤ <i>k</i> ≤ number of unique elements.</li>
<li>Input words contain only lowercase letters.</li>
</ol>
<p><b>Follow up:</b></p>
<ol>
<li>Try to solve it in <i>O</i>(<i>n</i> log <i>k</i>) time and <i>O</i>(<i>n</i>) extra space.</li>
</ol>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Idea:</strong></p>
<p>Priority queue / min heap</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/692-ep94.png"><img class="alignnone size-full wp-image-666" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/692-ep94.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/692-ep94.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/692-ep94-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/692-ep94-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/692-ep94-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution</strong></p>
<p>C++ / priority_queue O(n log k) / O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 13 ms (&lt; 90.31 %)
class Solution {
private:
    typedef pair&lt;string, int&gt; Node;
    typedef function&lt;bool(const Node&amp;, const Node&amp;)&gt; Compare;
public:
    vector&lt;string&gt; topKFrequent(vector&lt;string&gt;&amp; words, int k) {
        unordered_map&lt;string, int&gt; count;
        for (const string&amp; word : words)
            ++count[word];
        
        Compare comparator = [](const Node&amp; a, const Node&amp; b) {
            // order by alphabet ASC
            if (a.second == b.second) 
                return  a.first &lt; b.first;
            // order by freq DESC
            return a.second &gt; b.second;
        };
        
        // Min heap by frequency
        priority_queue&lt;Node, vector&lt;Node&gt;, Compare&gt; q(comparator);
        
        // O(n*logk)
        for (const auto&amp; kv : count) {
            q.push(kv);
            if (q.size() &gt; k) q.pop();
        }
        
        vector&lt;string&gt; ans;
        
        while (!q.empty()) {
            ans.push_back(q.top().first);
            q.pop();
        }
        
        std::reverse(ans.begin(), ans.end());
        return ans;
    }
};</pre><p><strong>Related Problems</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-347-top-k-frequent-elements/">[解题报告] LeetCode 347. Top K Frequent Elements</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/heap/leetcode-692-top-k-frequent-words/">花花酱 LeetCode 692. Top K Frequent Words</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/heap/leetcode-692-top-k-frequent-words/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 295. Find Median from Data Stream O(logn) + O(1)</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 12 Sep 2017 08:01:52 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Heap]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[median]]></category>
		<category><![CDATA[online]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[stream]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=243</guid>

					<description><![CDATA[<p>Problem: Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/">花花酱 LeetCode 295. Find Median from Data Stream O(logn) + O(1)</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><iframe width="500" height="375" src="https://www.youtube.com/embed/60xnYZ21Ir0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.</p>
<p>Examples:</p>
<p><code>[2,3,4]</code> , the median is <code>3</code></p>
<p><code>[2,3]</code>, the median is <code>(2 + 3) / 2 = 2.5</code></p>
<p>Design a data structure that supports the following two operations:</p>
<ul>
<li>void addNum(int num) &#8211; Add a integer number from the data stream to the data structure.</li>
<li>double findMedian() &#8211; Return the median of all elements so far.</li>
</ul>
<p>For example:</p><pre class="crayon-plain-tag">addNum(1)
addNum(2)
findMedian() = 1.5
addNum(3) 
findMedian() = 2</pre><p>&nbsp;</p>
<p><strong>Idea</strong>:</p>
<ol>
<li>Min/Max heap</li>
<li>Balanced binary search tree</li>
</ol>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1.png"><img class="alignnone size-full wp-image-248" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a><img class="alignnone size-full wp-image-247" style="font-size: 1rem;" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3.png"><img class="alignnone size-full wp-image-246" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/295-ep51-3-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Time Complexity</strong>:</p>
<p>add(num): O(logn)</p>
<p>findMedian(): O(logn)</p>
<p><strong>Solution1</strong>:</p><pre class="crayon-plain-tag">// Author: Huahua
// Running Time: 152 ms
class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder() {}
    
    // l_.size() &gt;= r_.size()
    void addNum(int num) {
        if (l_.empty() || num &lt;= l_.top()) {
            l_.push(num);
        } else {
            r_.push(num);
        }
        
        // Step 2: Balence left/right
        if (l_.size() &lt; r_.size()) {
            l_.push(r_.top());
            r_.pop();
        } else if (l_.size() - r_.size() == 2) {
            r_.push(l_.top());
            l_.pop();
        }
    }
    
    double findMedian() {
        if (l_.size() &gt; r_.size()) {
            return static_cast&lt;double&gt;(l_.top());
        } else {            
            return (static_cast&lt;double&gt;(l_.top()) + r_.top()) / 2;
        }
    }
private:
    priority_queue&lt;int, vector&lt;int&gt;, less&lt;int&gt;&gt; l_;    // max-heap
    priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; r_; // min-heap
};</pre><p>&nbsp;</p>
<p><strong>Solution 2:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 172 ms
class MedianFinder {
public:
    /** initialize your data structure here. */
    MedianFinder(): l_(m_.cend()), r_(m_.cend()) {}
    
    // O(logn)
    void addNum(int num) {
        if (m_.empty()) {
            l_ = r_ = m_.insert(num);
            return;
        }
        
        m_.insert(num);
        const size_t n = m_.size();    
        
        if (n &amp; 1) {
            // odd number
            if (num &gt;= *r_) {         
                l_ = r_;
            } else {
                // num &lt; *r_, l_ could be invalidated
                l_ = --r_;
            }
        } else {
            if (num &gt;= *r_)
                ++r_;
            else
                --l_;
        }
    }
    // O(1)
    double findMedian() {
        return (static_cast&lt;double&gt;(*l_) + *r_) / 2;
    }
private:
    multiset&lt;int&gt; m_;
    multiset&lt;int&gt;::const_iterator l_;  // current left median
    multiset&lt;int&gt;::const_iterator r_;  // current right median
};</pre><p>&nbsp;</p>
<p><strong>Related Problems</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/difficulty/hard/leetcode-480-sliding-window-median/">花花酱 LeetCode 480. Sliding Window Median</a></li>
<li><a href="http://zxi.mytechroad.com/blog/binary-search/leetcode-4-median-of-two-sorted-arrays/">[解题报告] LeetCode 4. Median of Two Sorted Arrays</a></li>
<li><a href="http://zxi.mytechroad.com/blog/zoj/zoj-3612-median/">[ZOJ] 3612: Median</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-295-find-median-from-data-stream/">花花酱 LeetCode 295. Find Median from Data Stream O(logn) + O(1)</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/leetcode/leetcode-295-find-median-from-data-stream/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
