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 items where items[i] = [pricei, beautyi]
• An array queries where each queries[j] represents your maximum budget

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

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
🛍️ Luxury Boutique OptimizationOrganized Shelves (Sorted by Price)$1 SectionBeauty: 2Max so far: 2$2 SectionBeauty: 4Max so far: 4$3 SectionBeauty: 5Max so far: 5$5 SectionBeauty: 6Max so far: 6Customer Query: Budget = $3🔍 Binary Search: Find rightmost item ≤ $3✨ RecommendationMax Beauty: 5⚡ Instant response using precomputed data!
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!
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
52.8K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen