File tree 3 files changed +86
-0
lines changed
non-overlapping-intervals
remove-nth-node-from-end-of-list
3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ /**
3
+ * 알고리즘 복잡도
4
+ * - 시간 복잡도: O(nlogn)
5
+ * - 공간 복잡도: O(1)
6
+ */
7
+ function eraseOverlapIntervals ( intervals : number [ ] [ ] ) : number {
8
+ intervals . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] )
9
+ let end = intervals [ 0 ] [ 1 ]
10
+ let count = 0
11
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
12
+ if ( intervals [ i ] [ 0 ] < end ) {
13
+ count ++
14
+ } else {
15
+ end = intervals [ i ] [ 1 ]
16
+ }
17
+ }
18
+ return count
19
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
+ * }
11
+ */
12
+ /**
13
+ * n번째 노드 제거하기
14
+ * 알고리즘 복잡도
15
+ * - 시간 복잡도: O(n)
16
+ * - 공간 복잡도: O(n)
17
+ * @param head
18
+ * @param n
19
+ */
20
+ function removeNthFromEnd ( head : ListNode | null , n : number ) : ListNode | null {
21
+ let stack : ListNode [ ] = [ ] ;
22
+ let node = head ;
23
+
24
+ while ( node ) {
25
+ stack . push ( node ) ;
26
+ node = node . next ;
27
+ }
28
+
29
+ // 첫 번째 노드를 제거하는 경우 추가
30
+ if ( stack . length - n - 1 < 0 ) {
31
+ return head ?. next || null ;
32
+ }
33
+
34
+ const prevNode = stack [ stack . length - n - 1 ] ;
35
+ prevNode . next = prevNode . next ?. next || null ;
36
+
37
+ return head ;
38
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * class TreeNode {
4
+ * val: number
5
+ * left: TreeNode | null
6
+ * right: TreeNode | null
7
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ * }
13
+ */
14
+ /**
15
+ * 같은 트리인지 확인하기
16
+ * 알고리즘 복잡도
17
+ * - 시간 복잡도: O(n)
18
+ * - 공간 복잡도: O(n)
19
+ * @param p
20
+ * @param q
21
+ */
22
+ function isSameTree ( p : TreeNode | null , q : TreeNode | null ) : boolean {
23
+ if ( ! p || ! q ) {
24
+ return p === q ;
25
+ }
26
+
27
+ return p . val === q . val && isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right )
28
+
29
+ }
You can’t perform that action at this time.
0 commit comments