Most Beautiful Item for Each Query

Medium Topics Company Tags

Each entry of items is [price, beauty]. For every budget in queries, find the greatest beauty among items whose price is at most that budget. Return 0 for a budget that cannot afford any item. Return answers in the original query order. Each query is independent: answering one query does not remove an item.

Example 1:

Input: items = [[2,5],[4,3],[4,9]]
queries = [1,4,2]

Output: [0,9,5]

Explanation: Budget 1 cannot afford an item, so its answer is 0. Budget 4 can afford every item and gets beauty 9. Budget 2 can afford only [2,5], giving beauty 5. Keep the query order: [0,9,5].

Example 2:

Input: items = [[3,7],[3,2],[3,10]]
queries = [3,2,3]

Output: [10,0,10]

Explanation: All three items cost 3. A budget of 3 can choose beauty 10, while a budget of 2 cannot buy anything. Repeating budget 3 gives the same answer, producing [10,0,10].

Example 3:

Input: items = [[5,4],[8,2]]
queries = [8,100,5]

Output: [4,4,4]

Explanation: The item with price 5 has beauty 4, which is greater than the more expensive item's beauty 2. Every query can afford the price-5 item, so all three answers are 4.

Constraints:

  • 1 <= items.length, queries.length <= 100000
  • items[i].length == 2
  • 1 <= price, beauty, queries[j] <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.