add: 220220 [cpp]

This commit is contained in:
Lam Haoyin 2022-02-21 01:29:57 +08:00
parent 705d3e255f
commit b40a7393cc
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 32 additions and 1 deletions

31
cpp/2202/220220.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
#include <algorithm>
class Solution {
public:
static int removeCoveredIntervals(std::vector<std::vector<int>>& intervals) {
std::sort(intervals.begin(), intervals.end(), [](const std::vector<int>& x, const std::vector<int>& y) { return (x[1] - x[0]) < (y[1] - y[0]); });
int n = intervals.size(), ret = n;
bool* const vis = new bool[n]{};
for (int i = 0; i < n; ++i) {
auto const& current = intervals[i];
for (int j = 0; j < i; ++j) {
auto const& prev = intervals[j];
// prev is included in current
if (!vis[j] && prev[0] >= current[0] && prev[1] <= current[1]) {
vis[j] = true;
--ret;
}
}
}
delete[] vis;
return ret;
}
};
int main() {
std::vector<std::vector<int>> args {{1, 4}, {3, 6}, {2, 8}};
std::cout << Solution::removeCoveredIntervals(args);
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220219.cpp)
ADD_EXECUTABLE(2202 220220.cpp)