forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoobing.ts
More file actions
32 lines (27 loc) Β· 885 Bytes
/
soobing.ts
File metadata and controls
32 lines (27 loc) Β· 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* λ¬Έμ μ€λͺ
* - μ£Όμ΄μ§ μκ° κ°κ²©μ λν΄ νμλ₯Ό μ°Έμν μ μλμ§ μ¬λΆλ₯Ό λ°ννλ λ¬Έμ
*
* μμ΄λμ΄
* 1) μμ μκ°μ κΈ°μ€μΌλ‘ μ λ ¬ ν, μ΄μ νμμ μ’
λ£ μκ°κ³Ό νμ¬ νμμ μμ μκ°μ λΉκ΅νμ¬ μ°Έμ κ°λ₯ μ¬λΆλ₯Ό νλ¨
*
*/
function insert(intervals: number[][], newInterval: number[]): number[][] {
const result: number[][] = [];
let i = 0;
while (i < intervals.length && newInterval[0] > intervals[i][1]) {
result.push(intervals[i]);
i++;
}
while (i < intervals.length && newInterval[1] >= intervals[i][0]) {
newInterval[0] = Math.min(intervals[i][0], newInterval[0]);
newInterval[1] = Math.max(intervals[i][1], newInterval[1]);
i++;
}
result.push(newInterval);
while (i < intervals.length) {
result.push(intervals[i]);
i++;
}
return result;
}