forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMost Beautiful Item for Each Query.cpp
47 lines (41 loc) · 1.12 KB
/
Most Beautiful Item for Each Query.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {
sort(items.begin(),items.end());
int maxi = items[0][1];
// for(auto xt : items)
// {
// cout<<xt[0]<<" "<<xt[1]<<endl;
// }
for(auto &xt : items)
{
maxi = max(maxi , xt[1]);
xt[1] = maxi;
}
// for(auto xt : items)
// {
// cout<<xt[0]<<" "<<xt[1]<<endl;
// }
vector<int>ans;
int n = items.size();
for(int key : queries){
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right) {
int mid = (right + left) / 2;
if (items[mid][0] <= key) {
count = mid + 1;
left = mid + 1;
}
else
right = mid - 1;
}
if(count==0)
ans.push_back(0);
else
ans.push_back(items[count-1][1]);
}
return ans;
}
};