-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNon-overlapping Intervals.cpp
51 lines (44 loc) · 1.01 KB
/
Non-overlapping Intervals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int minRemoval(vector<vector<int>> &intervals) {
// code here
sort(intervals.begin(),intervals.end());
int cnt=0,prev=intervals[0][1];
for(int i=1;i<intervals.size();i++)
{
if(intervals[i][0]<prev)
{
cnt++;
prev = min(prev,intervals[i][1]);
}
else
{
prev = intervals[i][1];
}
}
return cnt;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin >> N;
vector<vector<int>> intervals(N, vector<int>(2));
for (int i = 0; i < N; i++) {
cin >> intervals[i][0] >> intervals[i][1];
}
Solution obj;
cout << obj.minRemoval(intervals) << endl;
cout << "~"
<< "\n";
}
return 0;
}
// } Driver Code Ends