[LeetCode] 2070. Most Beautiful Item for Each Query

CNoodle發表於2024-11-14

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)

相關文章