<?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>queries Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/queries/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/queries/</link>
	<description></description>
	<lastBuildDate>Sun, 14 Nov 2021 22:29:17 +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>queries Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/queries/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
