-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsolution.cpp
31 lines (28 loc) · 863 Bytes
/
solution.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
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
vector<int> maximumBeauty(vector<vector<int>> &items, vector<int> &queries)
{
// Sort items by price
sort(items.begin(), items.end());
// Prepare price-beauty pairs with running max beauty
vector<pair<int, int>> priceBeauty;
int maxBeauty = 0;
for (auto &item : items)
{
maxBeauty = max(maxBeauty, item[1]);
priceBeauty.push_back({item[0], maxBeauty});
}
// Process each query
vector<int> result;
for (int query : queries)
{
auto it = upper_bound(priceBeauty.begin(), priceBeauty.end(), make_pair(query, INT_MAX)) - 1;
result.push_back(it >= priceBeauty.begin() ? it->second : 0);
}
return result;
}
};