Skip to content

Commit

Permalink
Create non-overlapping-interval.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
saksham101 authored May 21, 2021
1 parent 7d22139 commit 4a02433
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions non-overlapping-interval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
int n = intervals.size();
int left = 0;
int right = 1;
int count = 0;
while(right < n) {
if(intervals[left][1] <= intervals[right][0]) {
left = right;
right += 1;
}
else if(intervals[left][1] <= intervals[right][1]) {
count += 1;
right += 1;
}
else if(intervals[left][1] > intervals[right][1]) {
count += 1;
left = right;
right += 1;
}
}
return count;
}
};

0 comments on commit 4a02433

Please sign in to comment.