diff --git a/Medium/56.Merge_Intervals.cpp b/Medium/56.Merge_Intervals.cpp new file mode 100644 index 0000000..f6b701a --- /dev/null +++ b/Medium/56.Merge_Intervals.cpp @@ -0,0 +1,48 @@ +/* +Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, + and return an array of the non-overlapping intervals that cover all the intervals in the input. + +Example 1: +Input: intervals = [[1,3],[2,6],[8,10],[15,18]] +Output: [[1,6],[8,10],[15,18]] + +1----3 => 1----6 + 2----6 + 8----10 => 8----10 + 15----18 => 15----18 + +*/ + + +class Solution { +public: + vector> merge(vector>& intervals) { + + vector> ans; + sort(intervals.begin(), intervals.end()); + + int n = intervals.size(); + for(int i=0; i