> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Most Beautiful Item for Each Query

> Tested Python solution for LeetCode 2070 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 2070, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/most-beautiful-item-for-each-query/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2070   # by problem number
lcpy gen -s most_beautiful_item_for_each_query   # by problem name
```

## Problem

You are given a 2D integer array items where items\[i] = \[price\<sub>i\</sub>, beauty\<sub>i\</sub>] denotes the \<b>price\</b> and \<b>beauty\</b> of an item respectively.

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

Return an array answer of the same length as queries where answer\[j] is the answer to the j\<sup>th\</sup> query.

### Examples

```
Input: items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
Output: [2,4,5,5,6,6]
Explanation:
- For queries[0]=1, [1,2] is the only item which has price <= 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.
```

```
Input: items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
Output: [4]
Explanation: 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.
```

```
Input: items = [[10,1000]], queries = [5]
Output: [0]
Explanation: 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.
```

### Constraints

* 1 \<= items.length, queries.length \<= 10^5
* items\[i].length == 2
* 1 \<= price\_i, beauty\_i, queries\[j] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/most_beautiful_item_for_each_query/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/most_beautiful_item_for_each_query/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bisect import bisect_right


class Solution:
    # Time: O(n log n + m log n) for n items and m queries
    # Space: O(n)
    def maximum_beauty(self, items: list[list[int]], queries: list[int]) -> list[int]:
        items = sorted(items)
        prices: list[int] = []
        best: list[int] = []
        max_beauty = 0
        for price, beauty in items:
            max_beauty = max(max_beauty, beauty)
            prices.append(price)
            best.append(max_beauty)
        result: list[int] = []
        for query in queries:
            i = bisect_right(prices, query)
            result.append(best[i - 1] if i > 0 else 0)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n + m log n) for n items and m queries | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.