fix(posts): fetch daily from files
This commit is contained in:
parent
3d2ee97afe
commit
2bcc3f4675
9 changed files with 691 additions and 0 deletions
28
public/code/algorithms/leetcode-daily/beauty.cpp
Normal file
28
public/code/algorithms/leetcode-daily/beauty.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
std::vector<int> maximumBeauty(std::vector<std::vector<int>>& items, std::vector<int>& queries) {
|
||||
std::sort(items.begin(), items.end());
|
||||
std::vector<std::pair<int, int>> sorted_queries;
|
||||
sorted_queries.reserve(queries.size());
|
||||
// couple queries with their indices
|
||||
for (size_t i = 0; i < queries.size(); ++i) {
|
||||
sorted_queries.emplace_back(queries[i], i);
|
||||
}
|
||||
std::sort(sorted_queries.begin(), sorted_queries.end());
|
||||
|
||||
int beauty = items[0][1];
|
||||
size_t i = 0;
|
||||
std::vector<int> ans(queries.size());
|
||||
|
||||
for (const auto [query, index] : sorted_queries) {
|
||||
while (i < items.size() && items[i][0] <= query) {
|
||||
beauty = std::max(beauty, items[i][1]);
|
||||
++i;
|
||||
}
|
||||
// invariant: items[i - 1] is the rightmost considerable item
|
||||
ans[index] = i > 0 && items[i - 1][0] <= query ? beauty : 0;
|
||||
}
|
||||
|
||||
return std::move(ans);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue