Skip to content

Commit 20eb9f1

Browse files
Added task 2070.
1 parent 6d98959 commit 20eb9f1

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2001_2100.s2070_most_beautiful_item_for_each_query;
2+
3+
// #Medium #Array #Sorting #Binary_Search
4+
// #2022_05_30_Time_53_ms_(96.04%)_Space_119.9_MB_(48.51%)
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
9+
public class Solution {
10+
public int[] maximumBeauty(int[][] items, int[] queries) {
11+
int[] res = new int[queries.length];
12+
Arrays.sort(items, Comparator.comparingInt((a) -> a[1]));
13+
for (int i = 0; i < res.length; i++) {
14+
res[i] = maxBeauty(items, queries[i]);
15+
}
16+
return res;
17+
}
18+
19+
private int maxBeauty(int[][] items, int query) {
20+
for (int i = items.length - 1; i >= 0; i--) {
21+
int price = items[i][0];
22+
int beauty = items[i][1];
23+
if (price <= query) {
24+
return beauty;
25+
}
26+
}
27+
return 0;
28+
}
29+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2070\. Most Beautiful Item for Each Query
2+
3+
Medium
4+
5+
You are given a 2D integer array `items` where <code>items[i] = [price<sub>i</sub>, beauty<sub>i</sub>]</code> denotes the **price** and **beauty** of an item respectively.
6+
7+
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`.
8+
9+
Return _an array_ `answer` _of the same length as_ `queries` _where_ `answer[j]` _is the answer to the_ <code>j<sup>th</sup></code> _query_.
10+
11+
**Example 1:**
12+
13+
**Input:** items = [[1,2],[3,2],[2,4],[5,6],[3,5]], queries = [1,2,3,4,5,6]
14+
15+
**Output:** [2,4,5,5,6,6]
16+
17+
**Explanation:** -
18+
19+
For queries[0]=1, [1,2] is the only item which has price <= 1. Hence, the answer for this query is 2.
20+
21+
- For queries[1]=2, the items which can be considered are [1,2] and [2,4].
22+
23+
The maximum beauty among them is 4.
24+
- For queries[2]=3 and queries[3]=4, the items which can be considered are [1,2], [3,2], [2,4], and [3,5].
25+
26+
The maximum beauty among them is 5.
27+
- For queries[4]=5 and queries[5]=6, all items can be considered.
28+
29+
Hence, the answer for them is the maximum beauty of all items, i.e., 6.
30+
31+
**Example 2:**
32+
33+
**Input:** items = [[1,2],[1,2],[1,3],[1,4]], queries = [1]
34+
35+
**Output:** [4]
36+
37+
**Explanation:** The price of every item is equal to 1, so we choose the item with the maximum beauty 4.
38+
39+
Note that multiple items can have the same price and/or beauty.
40+
41+
**Example 3:**
42+
43+
**Input:** items = [[10,1000]], queries = [5]
44+
45+
**Output:** [0]
46+
47+
**Explanation:** No item has a price less than or equal to 5, so no item can be chosen.
48+
49+
Hence, the answer to the query is 0.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= items.length, queries.length <= 10<sup>5</sup></code>
54+
* `items[i].length == 2`
55+
* <code>1 <= price<sub>i</sub>, beauty<sub>i</sub>, queries[j] <= 10<sup>9</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2001_2100.s2070_most_beautiful_item_for_each_query;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.CommonUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
public void maximumBeauty() {
12+
int[][] items =
13+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
14+
"[1,2],[3,2],[2,4],[5,6],[3,5]");
15+
int[] queries = new int[] {1, 2, 3, 4, 5, 6};
16+
int[] expected = new int[] {2, 4, 5, 5, 6, 6};
17+
assertThat(new Solution().maximumBeauty(items, queries), equalTo(expected));
18+
}
19+
20+
@Test
21+
public void maximumBeauty2() {
22+
int[][] items =
23+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
24+
"[1,2],[1,2],[1,3],[1,4]");
25+
int[] queries = new int[] {1};
26+
int[] expected = new int[] {4};
27+
assertThat(new Solution().maximumBeauty(items, queries), equalTo(expected));
28+
}
29+
30+
@Test
31+
public void maximumBeauty3() {
32+
int[][] items =
33+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[10,1000]");
34+
int[] queries = new int[] {5};
35+
int[] expected = new int[] {0};
36+
assertThat(new Solution().maximumBeauty(items, queries), equalTo(expected));
37+
}
38+
}

0 commit comments

Comments
 (0)