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 1
1
/**
2
- * Source: https://leetcode.com/problems/two-sum/
2
+ * Source: https://leetcode.com/problems/insert-interval/
3
+
3
4
* 풀이방법: Map을 이용하여 필요한 나머지 숫자를 저장하면서 확인
4
5
* 시간복잡도: O(n)
5
6
* 공간복잡도: O(n)
You can’t perform that action at this time.
0 commit comments