diff --git a/non-overlapping-interval.cpp b/non-overlapping-interval.cpp new file mode 100644 index 0000000..5367a1c --- /dev/null +++ b/non-overlapping-interval.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int eraseOverlapIntervals(vector>& 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; + } +};