[LeetCode] 2070. Most Beautiful Item for Each Query
You are given a 2D integer array items where items[i] = [pricei, beautyi] denotes the price and beauty of an item respectively.
You are also given a 0-indexed integer array queries. For each queries[j], you want to determine the maximum beauty of an item whose price is less than or equal 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 jth query.
Example 1:
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.
Example 2:
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.
Example 3:
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 <= 105
items[i].length == 2
1 <= pricei, beautyi, queries[j] <= 109
每一个查询的最大美丽值。
给你一个二维整数数组 items ,其中 items[i] = [pricei, beautyi] 分别表示每一个物品的 价格 和 美丽值 。同时给你一个下标从 0 开始的整数数组 queries 。对于每个查询 queries[j] ,你想求出价格小于等于 queries[j] 的物品中,最大的美丽值 是多少。如果不存在符合条件的物品,那么查询的结果为 0 。
请你返回一个长度与 queries 相同的数组 answer,其中 answer[j]是第 j 个查询的答案。
思路一
这道题整体的思路是排序 + 双指针。以下是细节。
input 二维整数数组 items ,其中 items[i] = [price, beauty],且我们最后要返回的是对于每个查询 queries[j],找到价格小于等于 queries[j]
的物品中,最大的美丽值
是多少。如果不对 input 数组排序,我们只能一个个看每个 item 的 price 是多少,然后再看每个 item 的价格是否满足要求以决定美丽值是多少。为了降低复杂度,我们可以根据 price 将 input 数组升序排列。根据 price 排列好的 items,我们可以用双指针来找满足价格 <= queries[j]
的物品中最大的美丽值。
复杂度
时间O(nlogn) + O(mn) = O(mn)
空间O(n) - output array
代码
Java实现
class Solution {
public int[] maximumBeauty(int[][] items, int[] queries) {
// sort by price
Arrays.sort(items, (a, b) -> a[0] - b[0]);
int[][] indexedQueries = new int[queries.length][2];
for (int i = 0; i < queries.length; i++) {
indexedQueries[i][0] = queries[i];
indexedQueries[i][1] = i;
}
// sort by query value
Arrays.sort(indexedQueries, (a, b) -> a[0] - b[0]);
int[] res = new int[queries.length];
int max = 0;
int i = 0;
for (int[] q : indexedQueries) {
int qValue = q[0];
int qIndex = q[1];
while (i < items.length && items[i][0] <= qValue) {
max = Math.max(max, items[i][1]);
i++;
}
res[qIndex] = max;
}
return res;
}
}
// sort + two pointer
// O(mn)
思路二
还是需要对 price 排序。排序过后我们需要遍历一遍 input 数组,对于相同 price 的物品,我们只需要保留最大的 beauty 值。这个保留最大的 beauty 值的过程,我们可以用一个变量 max 来记录。不过对于相同 price 的物品,最后他们的 beauty 并不是一样的,而只是非递减的。最后对于相同 price 的物品,他们的美丽值类似这样排列,[1,1,2,3,3,4,5,5,5,5]
。
接着我们用二分法。因为我们已经排过序,所以我们可以判断,如果 mid 的价格大于要找的价格,那么我们只需要看左半边,这样就去掉一半的物品了。这里就有点像 34 题,在有序数组里找元素的二分法。
复杂度
时间O(m * logn) - m 个 query, 每个 query 都需要二分法查找
空间O(n) - output array
代码
Java实现
class Solution {
public int[] maximumBeauty(int[][] items, int[] queries) {
int[] res = new int[queries.length];
// sort by price
Arrays.sort(items, (a, b) -> a[0] - b[0]);
int max = items[0][1];
for (int i = 0; i < items.length; i++) {
max = Math.max(max, items[i][1]);
items[i][1] = max;
}
for (int i = 0; i < queries.length; i++) {
res[i] = helper(items, queries[i]);
}
return res;
}
private int helper(int[][] items, int targetPrice) {
int left = 0;
int right = items.length - 1;
int maxBeauty = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if (items[mid][0] > targetPrice) {
right = mid - 1;
} else {
maxBeauty = Math.max(maxBeauty, items[mid][1]);
left = mid + 1;
}
}
return maxBeauty;
}
}
// sort + binary search
// O(nlogn)