File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์๋ก์ด interval์ ๊ธฐ์กด intervals ๋ฐฐ์ด์ ์ฝ์
ํ๊ณ ๊ฒน์น๋ ๊ตฌ๊ฐ์ ๋ณํฉํ์ฌ ๋ฐํํ๋ ํจ์
3+ * @param {number[][] } intervals - ๊ธฐ์กด interval ๋ฐฐ์ด
4+ * @param {number[] } newInterval - ์ถ๊ฐํ๋ ค๋ interval
5+ * @returns {number[][] } - ์๋ก์ด interval์ ์ถ๊ฐํ๊ณ ๋ณํฉํ ๋ฐฐ์ด
6+ *
7+ * ์๊ฐ๋ณต์ก๋ : O(n)
8+ * - intervals ๋ฐฐ์ด์ ์ํํ๋ฉด์ newInterval๊ณผ ๊ฒน์น๋ ๊ตฌ๊ฐ์ ์ฐพ์ ๋ณํฉ
9+ *
10+ * ๊ณต๊ฐ๋ณต์ก๋ : O(n)
11+ * - ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋ฐฐ์ด์ ์์ฑ.
12+ */
13+ function insert ( intervals : number [ ] [ ] , newInterval : number [ ] ) : number [ ] [ ] {
14+ // ๊ฒฐ๊ณผ ๋ฐฐ์ด์ newInterval ํ๋๋ง ๊ฐ์ง ๋ฐฐ์ด๋ก ์์
15+ const result : number [ ] [ ] = [ newInterval ] ;
16+
17+ for ( let i = 0 ; i < intervals . length ; i ++ ) {
18+ const newItem = intervals [ i ] ;
19+ const lastItem = result . pop ( ) ! ;
20+
21+ // ํ์ฌ ๊ตฌ๊ฐ์ด lastItem ๊ตฌ๊ฐ์ ์ผ์ชฝ์ ์์ ํ ์์นํ๋ ๊ฒฝ์ฐ
22+ if ( newItem [ 1 ] < lastItem [ 0 ] ) {
23+ result . push ( newItem ) ;
24+ result . push ( lastItem ) ;
25+ }
26+ // ํ์ฌ ๊ตฌ๊ฐ์ด lastItem ๊ตฌ๊ฐ์ ์ค๋ฅธ์ชฝ์ ์์ ํ ์์นํ๋ ๊ฒฝ์ฐ
27+ else if ( newItem [ 0 ] > lastItem [ 1 ] ) {
28+ result . push ( lastItem ) ;
29+ result . push ( newItem ) ;
30+ }
31+ // ๋ ๊ตฌ๊ฐ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ: ๋ณํฉํ์ฌ ํ๋์ ๊ตฌ๊ฐ์ผ๋ก ๋ณํฉ
32+ else {
33+ lastItem [ 0 ] = Math . min ( lastItem [ 0 ] , newItem [ 0 ] ) ;
34+ lastItem [ 1 ] = Math . max ( lastItem [ 1 ] , newItem [ 1 ] ) ;
35+ result . push ( lastItem ) ;
36+ }
37+ }
38+
39+ return result ;
40+ }
41+
You canโt perform that action at this time.
0 commit comments