-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.java
34 lines (30 loc) · 1.05 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
class Solution {
public int[] maximumBeauty(int[][] items, int[] queries) {
Arrays.sort(items, Comparator.comparingInt(a -> a[0]));
List<int[]> priceBeauty = new ArrayList<>();
int maxBeauty = 0;
for (int[] item : items) {
maxBeauty = Math.max(maxBeauty, item[1]);
priceBeauty.add(new int[] { item[0], maxBeauty });
}
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int query = queries[i];
int idx = binarySearch(priceBeauty, query);
result[i] = idx != -1 ? priceBeauty.get(idx)[1] : 0;
}
return result;
}
private int binarySearch(List<int[]> priceBeauty, int query) {
int left = 0, right = priceBeauty.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (priceBeauty.get(mid)[0] <= query)
left = mid + 1;
else
right = mid - 1;
}
return right;
}
}