2070. Most Beautiful Item for Each Query Medium

@problem@discussion
#Array#Binary Search#Sorting



1/**
2 * [2070] Most Beautiful Item for Each Query
3 *
4 * You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively.
5 * You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty of an item whose price is less than or equal to queries[j]. If no such item exists, then the answer to this query is 0.
6 * Return an array answer of the same length as queries where answer[j] is the answer to the j^th query.
7 *  
8 * Example 1:
9 * 
10 * Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
11 * Output: [2,4,5,5,6,6]
12 * Explanation:
13 * - For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2.
14 * - For queries[1]=2, the items which can be considered are [1,2] and [2,4]. 
15 *   The maximum beauty among them is 4.
16 * - For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
17 *   The maximum beauty among them is 5.
18 * - For queries[4]=5 and queries[5]=6, all items can be considered.
19 *   Hence, the answer for them is the maximum beauty of all items, i.e., 6.
20 * 
21 * Example 2:
22 * 
23 * Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
24 * Output: [4]
25 * Explanation: 
26 * The price of every item is equal to 1, so we choose the item with the maximum beauty 4. 
27 * Note that multiple items can have the same price and/or beauty.  
28 * 
29 * Example 3:
30 * 
31 * Input: items = [[10,1000]], queries = [5]
32 * Output: [0]
33 * Explanation:
34 * No item has a price less than or equal to 5, so no item can be chosen.
35 * Hence, the answer to the query is 0.
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	1 <= items.length, queries.length <= 10^5
41 * 	items[i].length == 2
42 * 	1 <= pricei, beautyi, queries[j] <= 10^9
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/most-beautiful-item-for-each-query/
48// discuss: https://leetcode.com/problems/most-beautiful-item-for-each-query/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn maximum_beauty(items: Vec<Vec<i32>>, queries: Vec<i32>) -> Vec<i32> {
54        vec![]
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_2070() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.