Skip to content

Commit

Permalink
Update 13. Overlapping Intervals.cpp
Browse files Browse the repository at this point in the history
shumbul authored Jan 1, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
333fred Fred Silberberg
1 parent 30709b7 commit 96ccf50
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 13. Overlapping Intervals.cpp
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

0 comments on commit 96ccf50

Please sign in to comment.