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 5
5
var merge = function ( intervals ) {
6
6
const sort = intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
7
7
8
- const arr = [ sort [ 0 ] ] ;
8
+ const mergedIntervals = [ sort [ 0 ] ] ;
9
9
10
10
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 ] ;
12
15
13
16
const next = sort [ i ] [ 0 ] ;
14
17
15
- if ( endOfArr < next ) {
16
- arr . push ( sort [ i ] ) ;
18
+ if ( endOfMergedInterval < next ) {
19
+ mergedIntervals . push ( sort [ i ] ) ;
17
20
} 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 ] ) ;
19
22
}
20
23
}
21
24
22
- return arr ;
25
+ return mergedIntervals ;
23
26
} ;
24
27
25
28
// 시간복잡도 O(nlogn) -> sort 함수의 시간복잡도가 O(nlogn)이기 때문에
You can’t perform that action at this time.
0 commit comments