File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
non-overlapping-intervals 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
+ * @description
3
+ * overlapping์ด ์๋๊ธฐ์ํ ๊ธฐ์ค์ด ํ์ํจ์ ๋๋
4
+ * ์ฒ์์๋ ์์์ , ๋์ ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ง๋ง 16๋ฒ ํ
์คํธ์์ ์คํจ
5
+ * ์ ๋ ฌ๊ธฐ์ค์ด ๋์ , ์์์ ์์ผ๋ก ์ ๋ ฌํด์ผํ๋ค๊ณ ๊นจ๋ซ๊ฒ ๋จ
6
+ *
7
+ * n = length of intervals
8
+ * time complexity: O(n log n)
9
+ * space complexity: O(n)
10
+ */
11
+ var eraseOverlapIntervals = function ( intervals ) {
12
+ intervals . sort ( ( a , b ) => {
13
+ if ( a [ 1 ] !== b [ 1 ] ) return a [ 1 ] - b [ 1 ] ;
14
+ if ( a [ 0 ] !== b [ 0 ] ) return b [ 0 ] - a [ 0 ] ;
15
+ return 0 ;
16
+ } ) ;
17
+
18
+ let answer = 0 ;
19
+ const current = intervals [ 0 ] ;
20
+
21
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
22
+ const [ start , end ] = intervals [ i ] ;
23
+
24
+ if ( current [ 1 ] > start ) answer ++ ;
25
+ else {
26
+ current [ 0 ] = start ;
27
+ current [ 1 ] = end ;
28
+ }
29
+ }
30
+
31
+ return answer ;
32
+ } ;
You canโt perform that action at this time.
0 commit comments