File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Source: https://leetcode.com/problems/insert-interval/
3+ * ํ์ด๋ฐฉ๋ฒ: ๋ฏธ๋ฆฌ ์ ๋ ฌ ํ ๋ ๋ถ๋ถ๋ง ๋น๊ตํ๋ฉด์ ๊ฐฑ์ ์ํค๊ธฐ
4+ * ์๊ฐ๋ณต์ก๋: O(nlogn) - ์ ๋ ฌ๋๋ฌธ์
5+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
6+ *
7+ * ํต๊ณผ์๊ฐ
8+ * - ์ต์ด: 40๋ถ
9+ */
10+ function insert ( intervals : number [ ] [ ] , newInterval : number [ ] ) : number [ ] [ ] {
11+ const mergedIntervals = [ ...intervals , newInterval ] ;
12+ const result : number [ ] [ ] = [ ] ;
13+ mergedIntervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
14+
15+ for ( const interval of mergedIntervals ) {
16+ // ๊ฒฐ๊ณผ ๋ฐฐ์ด์ด ๋น์ด์๊ฑฐ๋ ํ์ฌ ๊ตฌ๊ฐ์ด ๋ง์ง๋ง ๊ตฌ๊ฐ๊ณผ ๊ฒน์น์ง ์๋ ๊ฒฝ์ฐ
17+ if ( result . length === 0 || interval [ 0 ] > result [ result . length - 1 ] [ 1 ] ) {
18+ result . push ( interval ) ;
19+ } else {
20+ // ๊ฒฐ๊ณผ๋ฌผ์ ๋ง์ง๋ง ๊ตฌ๊ฐ์ ํฐ๊ฐ ๋ฒ์์ ํ์ฌ ๊ตฌ๊ฐ์ ํฐ๊ฐ ๋ฒ์๋ฅผ ๋น๊ตํ ํฐ ๊ฐ์ผ๋ก ๋์ฒดํ๊ธฐ
21+ result [ result . length - 1 ] [ 1 ] = Math . max (
22+ result [ result . length - 1 ] [ 1 ] ,
23+ interval [ 1 ]
24+ ) ;
25+ }
26+ }
27+
28+ return result ;
29+ }
Original file line number Diff line number Diff line change 11/**
2- * Source: https://leetcode.com/problems/two-sum/
2+ * Source: https://leetcode.com/problems/insert-interval/
3+
34 * ํ์ด๋ฐฉ๋ฒ: Map์ ์ด์ฉํ์ฌ ํ์ํ ๋๋จธ์ง ์ซ์๋ฅผ ์ ์ฅํ๋ฉด์ ํ์ธ
45 * ์๊ฐ๋ณต์ก๋: O(n)
56 * ๊ณต๊ฐ๋ณต์ก๋: O(n)
You canโt perform that action at this time.
0 commit comments