#72 π 2070. Most Beautiful Item for Each Query π§ π
Imagine youβre in a store filled with beautiful items, each with a price and a beauty score. You have a limited budget for each shopping trip, and you want to get the most beautiful item that your money can buy! This problem is all about finding the maximum beauty within a price constraint for each query. Letβs explore how we can efficiently solve this with Python!
π Problem Statement
Given:
items
: A 2D list whereitems[i] = [price_i, beauty_i]
represents the price and beauty of an item.queries
: A list where eachqueries[j]
represents a budget. For each query, find the maximum beauty of an item with a price less than or equal toqueries[j]
. If no item meets the budget, the answer for that query is0
.
Return an array answer
, where answer[j]
is the solution for queries[j]
.
π Example
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
, the max beauty among items withprice <= 1
is2
. - For
queries[1] = 2
, items withprice <= 2
have max beauty4
. - For
queries[2] = 3
, items withprice <= 3
have max beauty5
. - And so onβ¦
π§ Approach
Weβll explore two solutions:
- Naive Solution: Checking each item for each query.
- Optimized Solution: Using sorting and prefix maximums for efficiency.
π‘ Basic Solution (Brute Force) π‘
Steps
For each query:
- Traverse the
items
list. - For each item that fits within the budget, track the maximum beauty.
- Append the result to the answer list.
π°οΈ Time Complexity
- Worst-case Time Complexity: \(O(n \times m)\), where \(n\) is the length of
queries
and \(m\) is the length ofitems
. - This is inefficient for large inputs as it involves re-checking each item for every query.
π₯ Code Implementation (Brute Force)
def mostBeautifulItemBruteForce(items, queries):
answer = []
for q in queries:
max_beauty = 0
for price, beauty in items:
if price <= q:
max_beauty = max(max_beauty, beauty)
answer.append(max_beauty)
return answer
βοΈ Optimized Solution βοΈ
To improve efficiency, letβs leverage sorting and prefix maximums.
Steps
- Sort both
items
by price andqueries
while keeping track of their original indices. - Build a max beauty prefix for
items
, whereprefix[i]
stores the maximum beauty from the start up toitems[i]
. - For each sorted query:
- Use a pointer to move through the items within the price limit of the current query.
- Store the maximum beauty from
prefix
that can be bought with the query budget.
- Restore the original order of queries.
π°οΈ Time Complexity
- Optimized Time Complexity: \(O(m \log m + n \log n)\), where \(m\) and \(n\) are the lengths of
items
andqueries
, respectively. - This is feasible for large input sizes due to reduced re-checks by leveraging sorted data.
π Code Implementation (Optimized Solution)
def mostBeautifulItemOptimized(items, queries):
# Step 1: Sort items by price
items.sort()
# Step 2: Sort queries while keeping track of their indices
sorted_queries = sorted((q, i) for i, q in enumerate(queries))
# Step 3: Prepare answer array and prefix variables
answer = [0] * len(queries)
max_beauty, j = 0, 0
# Step 4: Traverse queries in sorted order
for q_value, q_index in sorted_queries:
# Expand max beauty up to the current query price
while j < len(items) and items[j][0] <= q_value:
max_beauty = max(max_beauty, items[j][1])
j += 1
# Record the answer for this query's index
answer[q_index] = max_beauty
return answer
π Example Walkthrough
Using the optimized solution with items = [[1,2],[3,2],[2,4],[5,6],[3,5]]
and queries = [1,2,3,4,5,6]
.
- Sort
items
by price:items = [[1,2], [2,4], [3,2], [3,5], [5,6]]
- Sort
queries
with indices:sorted_queries = [(1,0), (2,1), (3,2), (4,3), (5,4), (6,5)]
- Traverse
sorted_queries
and useitems
up to each query:- For
q = 1
:items
up toprice 1
gives max beauty2
. - For
q = 2
: max beauty up toprice 2
is4
. - For
q = 3
: max beauty up toprice 3
is5
. - And so onβ¦
- For
The result is [2, 4, 5, 5, 6, 6]
.
π§© Edge Cases
- No items within budget: If
queries
has a value smaller than the lowest item price, return0
for those queries. - All items have the same price and beauty: Should correctly return the maximum beauty for all queries.
- Single item: Should handle cases where
items
has only one element. - Single query: Should handle cases with only one query.
π― Conclusion
In this post, we covered an efficient approach to answer multiple queries on finding the most beautiful item within budget constraints. By sorting both items
and queries
, and using a prefix maximum for beauty, we avoid redundant comparisons, achieving much faster performance. This problem highlights how sorting and prefix arrays can greatly reduce time complexity in range-query problems.