File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ ๋ ฌํ ๋ค์, ๋ง์ง๋ง ์์์ end๊ฐ์ ๊ฐ์ง๊ณ mergeํ ์ง ์์ ์ถ๊ฐํ ์ง๋ฅผ ๊ฒฐ์ ํฉ๋๋ค.
3
+ *
4
+ * TC: O(N * logN)
5
+ * ์ ๋ ฌ์ ์ํด N * logN ๋ณต์ก๋๋ฅผ ๊ฐ์ต๋๋ค.
6
+ *
7
+ * SC: O(1)
8
+ * N: intervals.length
9
+ */
10
+
11
+ /**
12
+ * @param {number[][] } intervals
13
+ * @return {number[][] }
14
+ */
15
+ var merge = function ( intervals ) {
16
+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
17
+
18
+ return intervals . reduce ( ( result , current ) => {
19
+ if ( result . length === 0 ) {
20
+ result . push ( current ) ;
21
+ return result ;
22
+ }
23
+
24
+ const previous = result [ result . length - 1 ] ;
25
+ if ( previous [ 1 ] >= current [ 0 ] ) {
26
+ result [ result . length - 1 ] [ 1 ] = Math . max ( current [ 1 ] , previous [ 1 ] ) ;
27
+ } else {
28
+ result . push ( current ) ;
29
+ }
30
+ return result ;
31
+ } , [ ] ) ;
32
+ } ;
You canโt perform that action at this time.
0 commit comments