Most Beautiful Item for Each Query - Problem
Find the Most Beautiful Item Within Budget!
Imagine you're shopping at a luxury boutique where each item has both a price and a beauty rating. You have multiple shopping queries, each with a different budget limit.
Given:
• A 2D array
• An array
Your task: For each query budget, find the maximum beauty value of any item you can afford (price ≤ budget). If no items are affordable, return 0.
Example: If items = [[1,2],[3,2],[2,4],[5,6],[3,5]] and queries = [1,2,3,4], then:
• Budget 1: Can afford item [1,2] → beauty = 2
• Budget 2: Can afford [1,2], [2,4] → max beauty = 4
• Budget 3: Can afford [1,2], [2,4], [3,2], [3,5] → max beauty = 5
• Budget 4: Same as budget 3 → max beauty = 5
Imagine you're shopping at a luxury boutique where each item has both a price and a beauty rating. You have multiple shopping queries, each with a different budget limit.
Given:
• A 2D array
items where items[i] = [pricei, beautyi]• An array
queries where each queries[j] represents your maximum budgetYour task: For each query budget, find the maximum beauty value of any item you can afford (price ≤ budget). If no items are affordable, return 0.
Example: If items = [[1,2],[3,2],[2,4],[5,6],[3,5]] and queries = [1,2,3,4], then:
• Budget 1: Can afford item [1,2] → beauty = 2
• Budget 2: Can afford [1,2], [2,4] → max beauty = 4
• Budget 3: Can afford [1,2], [2,4], [3,2], [3,5] → max beauty = 5
• Budget 4: Same as budget 3 → max beauty = 5
Input & Output
example_1.py — Basic Case
$
Input:
items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4]
›
Output:
[2,4,5,5]
💡 Note:
Query 1: Only [1,2] is affordable → beauty = 2. Query 2: [1,2] and [2,4] are affordable → max beauty = 4. Query 3: [1,2], [2,4], [3,2], [3,5] are affordable → max beauty = 5. Query 4: Same items as query 3 → max beauty = 5.
example_2.py — No Affordable Items
$
Input:
items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
›
Output:
[4]
💡 Note:
All items have price 1, so with budget 1, we can afford all of them. The maximum beauty among items with the same price is 4.
example_3.py — Budget Too Low
$
Input:
items = [[10,1000]], queries = [5]
›
Output:
[0]
💡 Note:
The only item costs 10, but our budget is 5, so we can't afford anything. Return 0.
Constraints
- 1 ≤ items.length ≤ 105
- items[i].length == 2
- 1 ≤ pricei, beautyi ≤ 109
- 1 ≤ queries.length ≤ 105
- 1 ≤ queries[j] ≤ 109
- Large dataset optimization required
Visualization
Tap to expand
Understanding the Visualization
1
Organize by Price
Arrange all items on shelves sorted by price from lowest to highest
2
Create Beauty Guide
For each price point, note the maximum beauty available up to that price
3
Quick Customer Service
When a customer states their budget, use binary search to instantly find the best recommendation
Key Takeaway
🎯 Key Insight: By sorting items and precomputing maximum beauty prefixes, we transform an O(n×m) brute force solution into an efficient O((n+m) log n) solution using binary search!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code