<?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>treemap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/treemap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/treemap/</link>
	<description></description>
	<lastBuildDate>Fri, 28 Mar 2025 03:46:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.9</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>treemap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/treemap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 3408. Design Task Manager</title>
		<link>https://zxi.mytechroad.com/blog/priority-queue/leetcode-3408-design-task-manager/</link>
					<comments>https://zxi.mytechroad.com/blog/priority-queue/leetcode-3408-design-task-manager/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 28 Mar 2025 03:45:03 +0000</pubDate>
				<category><![CDATA[Priority Queue]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[treemap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10229</guid>

					<description><![CDATA[<p>pq_ 使用std::map充当优先队列，存储(priority, taskId) -> userId m_ 使用std::unordered_map，存储taskId -> pq_的迭代器 所有操作都是O(logn) [crayon-67e74ace72700089430058/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-3408-design-task-manager/">花花酱 LeetCode 3408. Design Task Manager</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<ol><li>pq_ 使用std::map充当优先队列，存储(priority, taskId) -> userId</li><li>m_ 使用std::unordered_map，存储taskId -> pq_的迭代器</li></ol>



<p>所有操作都是O(logn)</p>



<pre class="crayon-plain-tag">class TaskManager {
public:
    TaskManager(vector&lt;vector&lt;int&gt;&gt;&amp; tasks) {
      for (const auto&amp; task : tasks)
        add(task[0], task[1], task[2]);
    }

    void add(int userId, int taskId, int priority) {
      m_[taskId] = pq_.emplace(make_pair(priority, taskId), userId).first;
    }
    
    void edit(int taskId, int newPriority) {
      const int userId = m_[taskId]-&gt;second;
      rmv(taskId);
      add(userId, taskId, newPriority);
    }
    
    void rmv(int taskId) {
      auto it = m_.find(taskId);
      pq_.erase(it-&gt;second);
      m_.erase(it);
    }
    
    int execTop() {
      if (pq_.empty()) return -1;
      auto it = pq_.rbegin();
      const int userId = it-&gt;second;
      const int taskId = (it-&gt;first.second);
      rmv(taskId);
      return userId;
    }
  private:
    map&lt;std::pair&lt;int,int&gt;, int&gt; pq_; // (priority, taskId) -&gt; userId;
    unordered_map&lt;int, map&lt;std::pair&lt;int,int&gt;, int&gt;::iterator&gt; m_; // taskId -&gt; pq iterator
};</pre>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-3408-design-task-manager/">花花酱 LeetCode 3408. Design Task Manager</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/priority-queue/leetcode-3408-design-task-manager/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2251. Number of Flowers in Full Bloom</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-2251-number-of-flowers-in-full-bloom/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-2251-number-of-flowers-in-full-bloom/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 28 Apr 2022 05:10:51 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[sweep line]]></category>
		<category><![CDATA[treemap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9692</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;2D integer array&#160;flowers, where&#160;flowers[i] = [starti, endi]&#160;means the&#160;ith&#160;flower will be in&#160;full bloom&#160;from&#160;starti&#160;to&#160;endi&#160;(inclusive). You are also given a&#160;0-indexed&#160;integer array&#160;persons&#160;of size&#160;n, where&#160;persons[i]&#160;is the time&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-2251-number-of-flowers-in-full-bloom/">花花酱 LeetCode 2251. Number of Flowers in Full Bloom</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&nbsp;<code>flowers</code>, where&nbsp;<code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>&nbsp;means the&nbsp;<code>i<sup>th</sup></code>&nbsp;flower will be in&nbsp;<strong>full bloom</strong>&nbsp;from&nbsp;<code>start<sub>i</sub></code>&nbsp;to&nbsp;<code>end<sub>i</sub></code>&nbsp;(<strong>inclusive</strong>). You are also given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>persons</code>&nbsp;of size&nbsp;<code>n</code>, where&nbsp;<code>persons[i]</code>&nbsp;is the time that the&nbsp;<code>i<sup>th</sup></code>&nbsp;person will arrive to see the flowers.</p>



<p>Return&nbsp;<em>an integer array&nbsp;</em><code>answer</code><em>&nbsp;of size&nbsp;</em><code>n</code><em>, where&nbsp;</em><code>answer[i]</code><em>&nbsp;is the&nbsp;<strong>number</strong>&nbsp;of flowers that are in full bloom when the&nbsp;</em><code>i<sup>th</sup></code><em>&nbsp;person arrives.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
<strong>Output:</strong> [1,2,2,2]
<strong>Explanation: </strong>The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> flowers = [[1,10],[3,3]], persons = [3,3,2]
<strong>Output:</strong> [2,2,1]
<strong>Explanation:</strong> The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.
</pre>



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



<ul><li><code>1 &lt;= flowers.length &lt;= 5 * 10<sup>4</sup></code></li><li><code>flowers[i].length == 2</code></li><li><code>1 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= persons.length &lt;= 5 * 10<sup>4</sup></code></li><li><code>1 &lt;= persons[i] &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Prefix Sum + Binary Search</strong></h2>



<p>Use a treemap to store the counts (ordered by time t), when a flower begins to bloom at start, we increase m[start], when it dies at end, we decrease m[end+1]. prefix_sum[t] indicates the # of blooming flowers at time t.</p>



<p>For each people, use binary search to find the latest # of flowers before his arrival.</p>



<p>Time complexity: O(nlogn + mlogn)<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:
  vector&lt;int&gt; fullBloomFlowers(vector&lt;vector&lt;int&gt;&gt;&amp; flowers, vector&lt;int&gt;&amp; persons) {
    map&lt;int, int&gt; m{{0, 0}};
    for (const auto&amp; f : flowers)
      ++m[f[0]], --m[f[1] + 1];    
    int sum = 0;
    for (auto&amp; [t, c] : m)
      c = sum += c;    
    vector&lt;int&gt; ans;    
    for (int t : persons)      
      ans.push_back(prev(m.upper_bound(t))-&gt;second);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-2251-number-of-flowers-in-full-bloom/">花花酱 LeetCode 2251. Number of Flowers in Full Bloom</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/tree/leetcode-2251-number-of-flowers-in-full-bloom/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2070. Most Beautiful Item for Each Query</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2070-most-beautiful-item-for-each-query/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2070-most-beautiful-item-for-each-query/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 14 Nov 2021 22:27:39 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[treemap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8699</guid>

					<description><![CDATA[<p>You are given a 2D integer array&#160;items&#160;where&#160;items[i] = [pricei, beautyi]&#160;denotes the&#160;price&#160;and&#160;beauty&#160;of an item respectively. You are also given a&#160;0-indexed&#160;integer array&#160;queries. For each&#160;queries[j], you want to&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2070-most-beautiful-item-for-each-query/">花花酱 LeetCode 2070. Most Beautiful Item for Each Query</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 2D integer array&nbsp;<code>items</code>&nbsp;where&nbsp;<code>items[i] = [price<sub>i</sub>, beauty<sub>i</sub>]</code>&nbsp;denotes the&nbsp;<strong>price</strong>&nbsp;and&nbsp;<strong>beauty</strong>&nbsp;of an item respectively.</p>



<p>You are also given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>queries</code>. For each&nbsp;<code>queries[j]</code>, you want to determine the&nbsp;<strong>maximum beauty</strong>&nbsp;of an item whose&nbsp;<strong>price</strong>&nbsp;is&nbsp;<strong>less than or equal</strong>&nbsp;to&nbsp;<code>queries[j]</code>. If no such item exists, then the answer to this query is&nbsp;<code>0</code>.</p>



<p>Return&nbsp;<em>an array&nbsp;</em><code>answer</code><em>&nbsp;of the same length as&nbsp;</em><code>queries</code><em>&nbsp;where&nbsp;</em><code>answer[j]</code><em>&nbsp;is the answer to the&nbsp;</em><code>j<sup>th</sup></code><em>&nbsp;query</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
<strong>Output:</strong> [2,4,5,5,6,6]
<strong>Explanation:</strong>
- For queries[0]=1, [1,2] is the only item which has price &lt;= 1. Hence, the answer for this query is 2.
- For queries[1]=2, the items which can be considered are [1,2] and [2,4]. 
  The maximum beauty among them is 4.
- For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
  The maximum beauty among them is 5.
- For queries[4]=5 and queries[5]=6, all items can be considered.
  Hence, the answer for them is the maximum beauty of all items, i.e., 6.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
<strong>Output:</strong> [4]
<strong>Explanation:</strong> 
The price of every item is equal to 1, so we choose the item with the maximum beauty 4. 
Note that multiple items can have the same price and/or beauty.  
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> items = [[10,1000]], queries = [5]
<strong>Output:</strong> [0]
<strong>Explanation:</strong>
No item has a price less than or equal to 5, so no item can be chosen.
Hence, the answer to the query is 0.
</pre>



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



<ul><li><code>1 &lt;= items.length, queries.length &lt;= 10<sup>5</sup></code></li><li><code>items[i].length == 2</code></li><li><code>1 &lt;= price<sub>i</sub>, beauty<sub>i</sub>, queries[j] &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Prefix Max + Binary Search</strong></h2>



<p>Sort items by price. For each price, use a treemap to store the max beauty of an item whose prices is &lt;= p. Then use binary search to find the max beauty whose price is &lt;= p.</p>



<p>Time complexity: Pre-processing O(nlogn) + query: O(qlogn)<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:
  vector&lt;int&gt; maximumBeauty(vector&lt;vector&lt;int&gt;&gt;&amp; items, vector&lt;int&gt;&amp; queries) {
    sort(begin(items), end(items));
    map&lt;int, int&gt; m;
    int best = 0;
    for (const auto&amp; item : items) {
      best = max(best, item[1]);
      m[item[0]] = best;
    }
    vector&lt;int&gt; ans;
    for (const int q: queries) {
      auto it = m.upper_bound(q);
      if (it == begin(m))
        ans.push_back(0);
      else
        ans.push_back(prev(it)-&gt;second);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-2070-most-beautiful-item-for-each-query/">花花酱 LeetCode 2070. Most Beautiful Item for Each Query</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/binary-search/leetcode-2070-most-beautiful-item-for-each-query/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>LeetCode 1801. Number of Orders in the Backlog</title>
		<link>https://zxi.mytechroad.com/blog/priority-queue/leetcode-1801-number-of-orders-in-the-backlog/</link>
					<comments>https://zxi.mytechroad.com/blog/priority-queue/leetcode-1801-number-of-orders-in-the-backlog/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 22 Mar 2021 06:08:26 +0000</pubDate>
				<category><![CDATA[Priority Queue]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[priority queue]]></category>
		<category><![CDATA[treemap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8266</guid>

					<description><![CDATA[<p>You are given a 2D integer array&#160;orders, where each&#160;orders[i] = [pricei, amounti, orderTypei]&#160;denotes that&#160;amountiorders have been placed of type&#160;orderTypei&#160;at the price&#160;pricei. The&#160;orderTypei&#160;is: 0&#160;if it is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-1801-number-of-orders-in-the-backlog/">LeetCode 1801. Number of Orders in the Backlog</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 2D integer array&nbsp;<code>orders</code>, where each&nbsp;<code>orders[i] = [price<sub>i</sub>, amount<sub>i</sub>, orderType<sub>i</sub>]</code>&nbsp;denotes that&nbsp;<code>amount<sub>i</sub></code>orders have been placed of type&nbsp;<code>orderType<sub>i</sub></code>&nbsp;at the price&nbsp;<code>price<sub>i</sub></code>. The&nbsp;<code>orderType<sub>i</sub></code>&nbsp;is:</p>



<ul><li><code>0</code>&nbsp;if it is a batch of&nbsp;<code>buy</code>&nbsp;orders, or</li><li><code>1</code>&nbsp;if it is a batch of&nbsp;<code>sell</code>&nbsp;orders.</li></ul>



<p>Note that&nbsp;<code>orders[i]</code>&nbsp;represents a batch of&nbsp;<code>amount<sub>i</sub></code>&nbsp;independent orders with the same price and order type. All orders represented by&nbsp;<code>orders[i]</code>&nbsp;will be placed before all orders represented by&nbsp;<code>orders[i+1]</code>&nbsp;for all valid&nbsp;<code>i</code>.</p>



<p>There is a&nbsp;<strong>backlog</strong>&nbsp;that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:</p>



<ul><li>If the order is a&nbsp;<code>buy</code>&nbsp;order, you look at the&nbsp;<code>sell</code>&nbsp;order with the&nbsp;<strong>smallest</strong>&nbsp;price in the backlog. If that&nbsp;<code>sell</code>&nbsp;order&#8217;s price is&nbsp;<strong>smaller than or equal to</strong>&nbsp;the current&nbsp;<code>buy</code>&nbsp;order&#8217;s price, they will match and be executed, and that&nbsp;<code>sell</code>&nbsp;order will be removed from the backlog. Else, the&nbsp;<code>buy</code>&nbsp;order is added to the backlog.</li><li>Vice versa, if the order is a&nbsp;<code>sell</code>&nbsp;order, you look at the&nbsp;<code>buy</code>&nbsp;order with the&nbsp;<strong>largest</strong>&nbsp;price in the backlog. If that&nbsp;<code>buy</code>&nbsp;order&#8217;s price is&nbsp;<strong>larger than or equal to</strong>&nbsp;the current&nbsp;<code>sell</code>&nbsp;order&#8217;s price, they will match and be executed, and that&nbsp;<code>buy</code>&nbsp;order will be removed from the backlog. Else, the&nbsp;<code>sell</code>&nbsp;order is added to the backlog.</li></ul>



<p>Return&nbsp;<em>the total&nbsp;<strong>amount</strong>&nbsp;of orders in the backlog after placing all the orders from the input</em>. Since this number can be large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p><strong>Example 1:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/ex1.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> Here is what happens with the orders:
- 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.
- 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.
- 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.
- 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3<sup>rd</sup> order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4<sup>th</sup> order is added to the backlog.
Finally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.
</pre>



<p><strong>Example 2:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/ex2.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
<strong>Output:</strong> 999999984
<strong>Explanation:</strong> Here is what happens with the orders:
- 10<sup>9</sup> orders of type sell with price 7 are placed. There are no buy orders, so the 10<sup>9</sup> orders are added to the backlog.
- 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog.
- 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.
- 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog.
Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (10<sup>9</sup> + 7).
</pre>



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



<ul><li><code>1 &lt;= orders.length &lt;= 10<sup>5</sup></code></li><li><code>orders[i].length == 3</code></li><li><code>1 &lt;= price<sub>i</sub>, amount<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>orderType<sub>i</sub></code>&nbsp;is either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2><strong>Solution: Treemap / PriorityQueue / Heap</strong></h2>



<p>buy backlog: max heap<br>sell backlog: min heap<br>Trade happens between the tops of two queues.</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 getNumberOfBacklogOrders(vector&lt;vector&lt;int&gt;&gt;&amp; orders) {  
    constexpr int kMod = 1e9 + 7;
    map&lt;int64_t, int64_t&gt; buys;
    map&lt;int64_t, int64_t&gt; sells;
    for (const auto&amp; order : orders) {      
      auto&amp; m = order[2] == 0 ? buys : sells;
      m[order[0]] += order[1];
      while (buys.size() &amp;&amp; sells.size()) {
        auto b = rbegin(buys);
        auto s = begin(sells);
        if (b-&gt;first &lt; s-&gt;first) break;
        const int k = min(b-&gt;second, s-&gt;second);
        if (!(b-&gt;second -= k)) buys.erase((++b).base());
        if (!(s-&gt;second -= k)) sells.erase(s);
      }
    }
    int64_t ans = 0;
    for (const auto&amp; [p, c] : buys) ans += c;
    for (const auto&amp; [p, c] : sells) ans += c;    
    return ans % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/priority-queue/leetcode-1801-number-of-orders-in-the-backlog/">LeetCode 1801. Number of Orders in the Backlog</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/priority-queue/leetcode-1801-number-of-orders-in-the-backlog/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1781. Sum of Beauty of All Substrings</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1781-sum-of-beauty-of-all-substrings/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1781-sum-of-beauty-of-all-substrings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 06 Mar 2021 17:41:22 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[frequency]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[treemap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8200</guid>

					<description><![CDATA[<p>The&#160;beauty&#160;of a string is the difference in frequencies between the most frequent and least frequent characters. For example, the beauty of&#160;"abaacc"&#160;is&#160;3 - 1 = 2.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1781-sum-of-beauty-of-all-substrings/">花花酱 LeetCode 1781. Sum of Beauty of All Substrings</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>The&nbsp;<strong>beauty</strong>&nbsp;of a string is the difference in frequencies between the most frequent and least frequent characters.</p>



<ul><li>For example, the beauty of&nbsp;<code>"abaacc"</code>&nbsp;is&nbsp;<code>3 - 1 = 2</code>.</li></ul>



<p>Given a string&nbsp;<code>s</code>, return&nbsp;<em>the sum of&nbsp;<strong>beauty</strong>&nbsp;of all of its substrings.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aabcb"
<strong>Output:</strong> 5
<strong>Explanation: </strong>The substrings with non-zero beauty are ["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aabcbaa"
<strong>Output:</strong> 17
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;=500</code></li><li><code>s</code>&nbsp;consists of only lowercase English letters.</li></ul>



<h2><strong>Solution: Treemap</strong></h2>



<p>Time complexity: O(n<sup>2</sup>log26)<br>Space complexity: O(26)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int beautySum(string_view s) {
    const int n = s.size();
    int ans = 0;
    vector&lt;int&gt; f(26);
    map&lt;int, int&gt; m;
    for (int i = 0; i &lt; n; ++i) {
      fill(begin(f), end(f), 0);
      m.clear();
      for (int j = i; j &lt; n; ++j) {
        const int c = ++f[s[j] - 'a'];
        ++m[c];
        if (c &gt; 1) {
          auto it = m.find(c - 1);
          if (--it-&gt;second == 0)
            m.erase(it);
        }
        ans += rbegin(m)-&gt;first - begin(m)-&gt;first;  
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1781-sum-of-beauty-of-all-substrings/">花花酱 LeetCode 1781. Sum of Beauty of All Substrings</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/string/leetcode-1781-sum-of-beauty-of-all-substrings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
