File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change 55var merge = function ( intervals ) {
66 const sort = intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
77
8- const arr = [ sort [ 0 ] ] ;
8+ const mergedIntervals = [ sort [ 0 ] ] ;
99
1010 for ( let i = 1 ; i < sort . length ; i ++ ) {
11- const endOfArr = arr [ arr . length - 1 ] [ 1 ] ;
11+ /** 현재 합쳐진 인터벌의 마지막 요소 */
12+ const lastMergedInterval = mergedIntervals [ mergedIntervals . length - 1 ] ;
13+
14+ const endOfMergedInterval = lastMergedInterval [ 1 ] ;
1215
1316 const next = sort [ i ] [ 0 ] ;
1417
15- if ( endOfArr < next ) {
16- arr . push ( sort [ i ] ) ;
18+ if ( endOfMergedInterval < next ) {
19+ mergedIntervals . push ( sort [ i ] ) ;
1720 } else {
18- arr [ arr . length - 1 ] [ 1 ] = Math . max ( arr [ arr . length - 1 ] [ 1 ] , sort [ i ] [ 1 ] ) ;
21+ lastMergedInterval [ 1 ] = Math . max ( lastMergedInterval [ 1 ] , sort [ i ] [ 1 ] ) ;
1922 }
2023 }
2124
22- return arr ;
25+ return mergedIntervals ;
2326} ;
2427
2528// 시간복잡도 O(nlogn) -> sort 함수의 시간복잡도가 O(nlogn)이기 때문에
You can’t perform that action at this time.
0 commit comments