File tree Expand file tree Collapse file tree 5 files changed +217
-0
lines changed
non-overlapping-intervals
number-of-connected-components-in-an-undirected-graph
remove-nth-node-from-end-of-list
serialize-and-deserialize-binary-tree Expand file tree Collapse file tree 5 files changed +217
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 구간이 겹치지 않게 하기 위해 최소한의 구간을 없애야 할 때, 몇 개를 없애야 하는지 반환하는 함수
3
+ * @param {number[][] } intervals
4
+ * @return {number }
5
+ */
6
+ const eraseOverlapIntervals = function ( intervals ) {
7
+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
8
+
9
+ let count = 0 ;
10
+ let prevEnd = intervals [ 0 ] [ 1 ] ;
11
+
12
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
13
+ const [ start , end ] = intervals [ i ] ;
14
+
15
+ // 범위가 겹치는 경우
16
+ if ( prevEnd > start ) {
17
+ count ++ ;
18
+ prevEnd = Math . min ( prevEnd , end ) ; // end가 큰 걸 삭제한다 치기
19
+ }
20
+ }
21
+
22
+ return count ;
23
+ } ;
24
+
25
+ // 시간복잡도: O(n * log n)
26
+ // 공간복잡도: O(1)
Original file line number Diff line number Diff line change
1
+
2
+ /**
3
+ * @param n: the number of vertices
4
+ * @param edges: the edges of undirected graph
5
+ * @return : the number of connected components
6
+ */
7
+ const countComponents = function ( n , edges ) {
8
+ // graph 만들기
9
+ const graph = Array . from ( { length : n } ) . map ( ( ) => [ ] ) ;
10
+
11
+ for ( const [ u , v ] of edges ) {
12
+ graph [ u ] . push ( v ) ;
13
+ graph [ v ] . push ( u ) ;
14
+ }
15
+
16
+ // 각 노드 순회하기
17
+ let count = 0 ;
18
+ const visited = new Set ( ) ;
19
+
20
+ for ( let i = 0 ; i < n ; i ++ ) {
21
+ if ( visited . has ( i ) ) {
22
+ continue ;
23
+ }
24
+
25
+ count += 1 ;
26
+
27
+ // bfs
28
+ const queue = [ i ] ;
29
+ visited . add ( i ) ;
30
+
31
+ while ( queue . length ) {
32
+ const u = queue . shift ( ) ;
33
+
34
+ for ( const v of graph [ u ] ) {
35
+ if ( ! visited . has ( v ) ) {
36
+ visited . add ( v ) ;
37
+ queue . push ( v ) ;
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ return count ;
44
+ }
45
+
46
+ // 시간복잡도: O(E)
47
+ // 공간복잡도: O(V)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+
9
+ // 첫 번째 풀이
10
+ // 시간복잡도: O(n)
11
+ // 공간복잡도: O(n)
12
+ /**
13
+ * @param {ListNode } head
14
+ * @param {number } n
15
+ * @return {ListNode }
16
+ */
17
+ const removeNthFromEnd = function ( head , n ) {
18
+ function dfs ( node ) {
19
+ // 마지막 노드이면 1 반환
20
+ if ( ! node . next ) {
21
+ return 1 ;
22
+ }
23
+
24
+ const nth = dfs ( node . next ) ;
25
+ if ( nth === n ) {
26
+ node . next = node . next . next ?? null ;
27
+ }
28
+
29
+ return nth + 1 ;
30
+ }
31
+
32
+ if ( dfs ( head ) === n ) {
33
+ return head . next ;
34
+ }
35
+
36
+ return head ;
37
+ } ;
38
+
39
+ // 두 번째 풀이
40
+ // 시간복잡도: O(n)
41
+ // 공간복잡도: O(1)
42
+ const removeNthFromEnd = function ( head , n ) {
43
+ const temp = new ListNode ( 0 , head ) ;
44
+ let tail = temp ;
45
+ let prev = temp ; // 뒤에서 n번째 노드
46
+
47
+ for ( let i = 0 ; i < n ; i ++ ) {
48
+ tail = tail . next ;
49
+ }
50
+
51
+ while ( tail . next ) {
52
+ tail = tail . next ;
53
+ prev = prev . next ;
54
+ }
55
+
56
+ prev . next = prev . next . next ;
57
+
58
+ return temp . next ;
59
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } p
11
+ * @param {TreeNode } q
12
+ * @return {boolean }
13
+ */
14
+ const isSameTree = function ( p , q ) {
15
+ if ( ! p && ! q ) return true ;
16
+ return p ?. val === q ?. val && isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
17
+ } ;
18
+
19
+ // 시간복잡도: O(n)
20
+ // 공간복잡도: O(h) (h: 트리의 높이, 즉 재귀 호출 스택)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+
9
+ const NULL_SIGN = 'X' ;
10
+
11
+ /**
12
+ * Encodes a tree to a single string.
13
+ * 시간복잡도: O(n)
14
+ * 공간복잡도: O(h) (h: 재귀 스택 깊이 즉 트리 높이)
15
+ * @param {TreeNode } root
16
+ * @return {string }
17
+ */
18
+ const serialize = function ( root ) {
19
+ const result = [ ] ;
20
+
21
+ function traverse ( root ) {
22
+ result . push ( root ?. val ?? NULL_SIGN ) ;
23
+ if ( ! root ) {
24
+ return ;
25
+ }
26
+ traverse ( root . left ) ;
27
+ traverse ( root . right ) ;
28
+ }
29
+
30
+ traverse ( root ) ;
31
+ return result . join ( ',' ) ;
32
+ } ;
33
+
34
+ /**
35
+ * Decodes your encoded data to tree.
36
+ * 시간복잡도: O(n)
37
+ * 공간복잡도: O(h) (h: 재귀 스택 깊이 즉 트리 높이)
38
+ * @param {string } data
39
+ * @return {TreeNode }
40
+ */
41
+ const deserialize = function ( data ) {
42
+ const splited = data . split ( ',' ) ;
43
+ let i = 0 ;
44
+
45
+ function makeTree ( ) {
46
+ if ( splited [ i ] === NULL_SIGN ) {
47
+ return null ;
48
+ }
49
+
50
+ const node = new TreeNode ( Number ( splited [ i ] ) ) ;
51
+ i += 1 ;
52
+ node . left = makeTree ( ) ;
53
+ i += 1 ;
54
+ node . right = makeTree ( ) ;
55
+
56
+ return node ;
57
+ }
58
+
59
+ return makeTree ( ) ;
60
+ } ;
61
+
62
+ /**
63
+ * Your functions will be called as such:
64
+ * deserialize(serialize(root));
65
+ */
You can’t perform that action at this time.
0 commit comments