File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-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 sort = intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
7+
8+ const arr = [ sort [ 0 ] ] ;
9+
10+ for ( let i = 1 ; i < sort . length ; i ++ ) {
11+ const endOfArr = arr [ arr . length - 1 ] [ 1 ] ;
12+
13+ const next = sort [ i ] [ 0 ] ;
14+
15+ if ( endOfArr < next ) {
16+ arr . push ( sort [ i ] ) ;
17+ } else {
18+ arr [ arr . length - 1 ] [ 1 ] = Math . max ( arr [ arr . length - 1 ] [ 1 ] , sort [ i ] [ 1 ] ) ;
19+ }
20+ }
21+
22+ return arr ;
23+ } ;
24+
25+ // ์๊ฐ๋ณต์ก๋ O(nlogn) -> sort ํจ์์ ์๊ฐ๋ณต์ก๋๊ฐ O(nlogn)์ด๊ธฐ ๋๋ฌธ์
26+ // ๊ณต๊ฐ๋ณต์ก๋ O(n) -> intervals ๋ฐฐ์ด์ ์ ๋ ฌํ์ฌ arr์ด๋ผ๋ ์๋ณ์์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ํ์ํ ๊ณต๊ฐ
You canโt perform that action at this time.
0 commit comments