File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } intervals
3+ * @return {number[][] }
4+ */
5+ var merge = function ( intervals ) {
6+ const sortedIntervals = intervals . toSorted ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
7+
8+ if ( intervals . length === 1 ) {
9+ return intervals ;
10+ }
11+
12+ const result = [ ] ;
13+ let [ start , end ] = sortedIntervals [ 0 ] ;
14+
15+
16+ for ( let i = 1 ; i < sortedIntervals . length ; i ++ ) {
17+ const [ currentStart , currentEnd ] = sortedIntervals [ i ] ;
18+
19+ if ( currentStart <= end ) {
20+ end = Math . max ( end , currentEnd ) ;
21+ } else {
22+ result . push ( [ start , end ] ) ;
23+ start = currentStart ;
24+ end = currentEnd ;
25+ }
26+
27+ if ( i === sortedIntervals . length - 1 ) {
28+ result . push ( [ start , end ] ) ;
29+ }
30+ }
31+
32+ return result ;
33+ } ;
34+
35+ // 시간 복잡도: O(nlogn)
36+ // 공간 복잡도: O(n)
You can’t perform that action at this time.
0 commit comments