-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_56.cpp
More file actions
23 lines (20 loc) · 867 Bytes
/
Copy pathleetcode_56.cpp
File metadata and controls
23 lines (20 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
if(intervals.size() <= 1) return intervals;
sort(intervals.begin(), intervals.end());
vector<vector<int>> res;
res.push_back(intervals[0]);
for (int i = 1; i < intervals.size(); i++) {
// 결과 벡터의 마지막 구간과 현재 구간 비교
if (intervals[i][0] <= res.back()[1]) {
// 겹치면 병합: 끝점을 더 큰 값으로 업데이트
res.back()[1] = max(res.back()[1], intervals[i][1]);
} else {
// 겹치지 않으면 그대로 추가
res.push_back(intervals[i]);
}
}
return res;
}
};