File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [][] merge (int [][] intervals ) {
3+
4+ if (intervals .length <= 1 ) {
5+ return intervals ;
6+ }
7+
8+ List <int []> answer = new ArrayList <>();
9+
10+ Arrays .sort (intervals , (a , b ) -> Integer .compare (a [0 ], b [0 ]));
11+
12+ int start = intervals [0 ][0 ];
13+ int end = intervals [0 ][1 ];
14+
15+ for (int i = 1 ; i < intervals .length ; i ++) {
16+
17+ // ํ์ฌ ์ธํฐ๋ฒ ์์์
18+ int currStart = intervals [i ][0 ];
19+ // ํ์ฌ ์ธํฐ๋ฒ ๋์
20+ int currEnd = intervals [i ][1 ];
21+
22+ if (end >= currStart ) {
23+ end = Math .max (end , currEnd );
24+ } else {
25+ answer .add (new int []{start , end });
26+ start = currStart ;
27+ end = currEnd ;
28+ }
29+ }
30+
31+ answer .add (new int []{start , end });
32+
33+ return answer .stream ()
34+ .toArray (int [][]::new );
35+ }
36+ }
37+
You canโt perform that action at this time.
0 commit comments