File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ 풀이 :
3
+ 시작시간으로 interval을 정렬한 뒤에 첫번째 interval을 ans에 넣는다(.back() 메서드의 에러 방지)
4
+ ans 배열의 마지막 interval과 시간이 겹치면 interval을 통합시키고
5
+ 겹치지 않으면 ans에 현재 interval을 넣고 반복
6
+
7
+ interval 개수 : N
8
+
9
+ TC : O (N logN)
10
+ sort로 정렬
11
+
12
+ SC : O (1)
13
+ ans외에 추가적인 메모리 할당은 상수
14
+ */
15
+
16
+ #include < vector>
17
+ #include < algorithm>
18
+ using namespace std ;
19
+
20
+ class Solution {
21
+ public:
22
+ vector<vector<int >> merge (vector<vector<int >>& intervals) {
23
+ vector<vector<int >> ans;
24
+
25
+ sort (intervals.begin (), intervals.end ());
26
+ ans.push_back (intervals[0 ]);
27
+
28
+ for (int i = 1 ; i < intervals.size (); i++) {
29
+ vector<int >& lastInterval = ans.back ();
30
+
31
+ if (intervals[i][0 ] <= lastInterval[1 ])
32
+ lastInterval[1 ] = max (lastInterval[1 ], intervals[i][1 ]);
33
+ else
34
+ ans.push_back (intervals[i]);
35
+ }
36
+ return ans;
37
+ }
38
+ };
You can’t perform that action at this time.
0 commit comments