Interval Scheduling Maximization
Algorithm
Implementation
// OJ: https://leetcode.com/problems/non-overlapping-intervals/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& A) {
sort(begin(A), end(A), [](vector<int> &a, vector<int> &b) { return a[1] < b[1]; });
int end = INT_MIN, ans = 0; // `end` is the maximum ending time of selected intervals
for (auto &x : A) {
if (x[0] >= end) end = x[1]; // this interval doesn't have overlap with the previously selected interval, select it and update the `end`.
else ++ans; // otherwise, overlapped intervals are skipped.
}
return ans;
}
};Problem
Reference
Last updated