add: 220520-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-20 18:48:14 +08:00
parent 3742f30fdc
commit 22018e1653
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 41 additions and 1 deletions

40
cpp/2205/220520-CN.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <vector>
#include <cstdio>
#include <tuple>
#include <algorithm>
/**
* 436. Find Right Interval
* You are given an array of intervals, where intervals[i] = [starti, endi] and each starti is unique.
* The right interval for an interval i is an interval j such that startj >= endi and startj is minimized. Note that i may equal j.
* Return an array of right interval indices for each interval i. If no right interval exists for interval i, then put -1 at index i.
*/
class Solution {
public:
static std::vector<int> findRightInterval(const std::vector<std::vector<int>>& intervals) {
std::vector<std::tuple<int, int, int>> s;
for (int i = 0; i < intervals.size(); ++i) {
s.emplace_back(intervals[i][0], intervals[i][1], i);
}
std::sort(s.begin(), s.end());
std::vector<int> ret(intervals.size());
for (const auto& [f, t, id] : s) {
auto it = std::lower_bound(s.begin(), s.end(), std::tuple<int, int, int>{t, -0x7FFFFFFF, -0x7FFFFFFF});
if (it == s.end())
ret[id] = -1;
else
ret[id] = std::get<2>(*it);
}
return ret;
}
};
int main() {
auto r = Solution::findRightInterval({{-100,-98},{-99,-97},{-98,-96},{-97,-95}});
for (int i : r) {
std::printf("%d ", i);
}
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220519.cpp) ADD_EXECUTABLE(2205 220520-CN.cpp)