Skip to content

Commit f58376b

Browse files
committed
Time: 24 ms (33.18%), Space: 14.7 MB (36.69%) - LeetHub
1 parent d21081e commit f58376b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

merge-intervals/merge-intervals.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
static bool compare(const vector<int> &v1, const vector<int> &v2){
4+
if(v1[0] < v2[0]){
5+
return true;
6+
}
7+
if(v1[0] > v2[0]){
8+
return false;
9+
}
10+
if(v1[1] < v2[1]){
11+
return true;
12+
}
13+
return false;
14+
}
15+
16+
vector<vector<int>> merge(vector<vector<int>>& intervals) {
17+
vector<vector<int>> ans;
18+
int ans_index = 0;
19+
sort(intervals.begin(), intervals.end(), compare);
20+
ans.push_back(intervals[0]);
21+
for(auto interval: intervals){
22+
if(ans[ans_index][1] >= interval[0]){
23+
ans[ans_index][1] = max(interval[1], ans[ans_index][1]);
24+
}
25+
else{
26+
ans_index++;
27+
ans.push_back(interval);
28+
}
29+
}
30+
return ans;
31+
}
32+
};

0 commit comments

Comments
 (0)