-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update 13. Overlapping Intervals.cpp
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,31 @@ | ||
// Problem link - https://bit.ly/48zi1vM | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> overlappedInterval(vector<vector<int>>& intervals) { | ||
// Code here | ||
sort(intervals.begin(), intervals.end()); | ||
int interval=0, n=intervals.size(); | ||
vector<vector<int>> ans; | ||
while(interval<n){ | ||
int start=intervals[interval][0]; | ||
int end=intervals[interval][1]; | ||
interval++; | ||
while(interval<n && intervals[interval][0]<=end){ | ||
end=max(end, intervals[interval][1]); | ||
interval++; | ||
} | ||
ans.push_back({start, end}); | ||
} | ||
return ans; | ||
} | ||
}; | ||
|
||
/* | ||
Time complexity: O(nlogn) - sorting | ||
Space complexity: O(n) | ||
*/ | ||
|
||
// Code by Shumbul Arifa - https://linktr.ee/shumbul | ||
// Follow 21 days DSA Challenge - www.shumbularifa.com | ||
// Video solutions available on my YouTube |