add: 220220 [cpp]
This commit is contained in:
parent
705d3e255f
commit
b40a7393cc
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2202 220219.cpp)
|
ADD_EXECUTABLE(2202 220220.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue