NeetCode #290LC-2070MediumBinary Search
← Back to All Problems#290 · #2070 · Most Beautiful Item for Each Query(每一个查询的最大美丽值)
📌 Problem Statement & Constraints
You are given a 2D array
items where items[i] = [price_i, beauty_i], and an array of queries. For each query, return the maximum beauty among all items with price at most that query. If no such item exists, return 0. Constraints: 1 <= items.length, queries.length <= 10^5, 1 <= price, beauty, query <= 10^9.💡 Core Algorithmic Approaches
- Sort the items by price. Then build a prefix maximum of beauty, so that
best[i]is the maximum beauty among the firsti + 1items. - For each query, binary search for the last item whose price is at most the query.
- The answer is the prefix maximum at that index, or 0 if no item qualifies.
- This gives O((n + q) log n), far better than a per-query linear scan.
💻 Benchmark Python3 Implementation
class Solution:
def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]:
from bisect import bisect_right
items.sort() # by price, then beauty
prices = []
best = []
cur = 0
for p, b in items:
cur = max(cur, b)
prices.append(p)
best.append(cur) # prefix maximum of beauty
res = []
for q in queries:
i = bisect_right(prices, q) - 1 # last item with price <= q
res.append(best[i] if i >= 0 else 0)
return res⚡ Complexity Deep Dive
⏱️ Time Complexity
O((n + q) log n): sorting plus a binary search per query.
💾 Space Complexity
O(n) for the prefix maxima.
⚠️ Interview Pitfalls & Follow-ups
- Scanning all items per query: O(n * q) = 10^10 operations, which times out.
- Storing the maximum beauty without the prefix structure: the query may admit several items, so the prefix maximum is required.
- Using
bisect_left: you want the last price at most the query, which isbisect_right(...) - 1. - Returning the beauty of the closest item rather than the maximum: the prefix maximum is the key.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Binary Search Problems
中文速览与面试要点
每一个查询的最大美丽值
给你一个二维整数数组 items,其中 items[i] = [pricei, beautyi] 分别表示每一个物品的 价格 和 美丽值 。同时给你一个下标从 0 开始的整数数组 queries。对于每个查询 queries[j] ,你想求出价格 小于等于 queries[j] 的物品中, 最大美丽值 。请你返回一个数组 answer,其中 answer[j] 是第 j 个查询的答案。约束:1 ≤ items.length, queries.length ≤ 10^5;1 ≤ pricei, beautyi, queries[j] ≤ 10^9。
核心要点
- 排序 + 前缀最大值 + 二分(标准解) :把 items 按价格升序排序,然后对美丽值求 前缀最大值 。
- 为什么用前缀最大值 :对固定价格上限 q,答案是「所有价格 ≤ q 的物品的美丽值最大值」,即前缀最大值的最后一项。
- 二分查询 :对每个 query,二分找到最后一个 price <= q 的位置,答案即该位置的前缀最大值。
- 复杂度 :排序 O(m log m),每个查询 O(log m),总计 O((m + q) log m)。
常见陷阱
- 对每个查询线性扫描所有物品 :O(m · q) 必然超时。必须排序 + 前缀最大值 + 二分。
- 用 bisect_left(prices, q) :会漏掉价格恰好等于 q 的物品。应用 bisect_right(prices, q) − 1 。
- 没有物品满足条件时返回 best[-1] :应返回 0(没有可购买的物品,美丽值为 0)。必须判断 idx >= 0 。
- 忘记前缀最大值只增不减 :排序后同一价格可能有多个物品,前缀最大值自然处理了这一点。
参考实现
from bisect import bisect_right
class Solution:
def maximumBeauty(self, items: list[list[int]], queries: list[int]) -> list[int]:
items.sort() # 按价格升序
prices = []
best = []
cur = 0
for price, beauty in items:
cur = max(cur, beauty) # 前缀最大美丽值
prices.append(price)
best.append(cur)
res = []
for q in queries:
# 找最后一个 price <= q 的位置
idx = bisect_right(prices, q) - 1
res.append(best[idx] if idx >= 0 else 0)
return res