File tree Expand file tree Collapse file tree
find-minimum-in-rotated-sorted-array
maximum-depth-of-binary-tree Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * bfs
3+ * 시간 복잡도: O(amount * n)
4+ * 공간 복잡도: O(amount)
5+ * @param {number[] } coins
6+ * @param {number } amount
7+ * @return {number }
8+ */
9+ var coinChange = function ( coins , amount ) {
10+ let q = [ ] ;
11+ let visited = new Set ( ) ;
12+ q . push ( [ 0 , 0 ] ) ;
13+ while ( q . length ) {
14+ const [ sum , count ] = q . shift ( ) ;
15+ if ( sum === amount ) return count ;
16+ if ( visited . has ( sum ) ) continue ;
17+ visited . add ( sum ) ;
18+ for ( let i = 0 ; i < coins . length ; i ++ ) {
19+ const nextSum = sum + coins [ i ] ;
20+ if ( nextSum <= amount ) {
21+ q . push ( [ nextSum , count + 1 ] ) ;
22+ }
23+ }
24+ }
25+ return - 1 ;
26+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ 이분 탐색
3+ 시간 복잡도 : O(log n)
4+ 공간 복잡도 : O(1)
5+ * @param {number[] } nums
6+ * @return {number }
7+ */
8+ var findMin = function ( nums ) {
9+ let left = 0 ;
10+ let right = nums . length - 1 ;
11+
12+ while ( left < right ) {
13+ let mid = Math . floor ( ( left + right ) / 2 ) ;
14+ if ( nums [ mid ] > nums [ right ] ) {
15+ left = mid + 1 ;
16+ } else {
17+ right = mid ;
18+ }
19+ }
20+
21+ return nums [ left ] ;
22+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ 재귀
3+ * Definition for a binary tree node.
4+ * function TreeNode(val, left, right) {
5+ * this.val = (val===undefined ? 0 : val)
6+ * this.left = (left===undefined ? null : left)
7+ * this.right = (right===undefined ? null : right)
8+ * }
9+ */
10+ /**
11+ * @param {TreeNode } root
12+ * @return {number }
13+ */
14+ var maxDepth = function ( root ) {
15+ if ( ! root ) return 0 ;
16+ return 1 + Math . max ( maxDepth ( root . left ) , maxDepth ( root . right ) ) ;
17+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 풀이
3+ * 더 작은 노드를 머리로 두고, 그 next에 나머지를 병합한 결과를 연결.
4+ * 최종적으로 머리를 반환
5+ *
6+ * 시간복잡도 - O(n + m) : n, m은 각각 list1, list2의 길이
7+ * 공간복잡도 - O(n + m) : 재귀 호출 스택
8+ *
9+ * Definition for singly-linked list.
10+ * function ListNode(val, next) {
11+ * this.val = (val===undefined ? 0 : val)
12+ * this.next = (next===undefined ? null : next)
13+ * }
14+ */
15+ /**
16+ * @param {ListNode } list1
17+ * @param {ListNode } list2
18+ * @return {ListNode }
19+ */
20+ var mergeTwoLists = function ( list1 , list2 ) {
21+ if ( ! list1 ) return list2 ;
22+ if ( ! list2 ) return list1 ;
23+
24+ if ( list1 . val <= list2 . val ) {
25+ list1 . next = mergeTwoLists ( list1 . next , list2 ) ;
26+ return list1 ;
27+ } else {
28+ list2 . next = mergeTwoLists ( list1 , list2 . next ) ;
29+ return list2 ;
30+ }
31+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ 현재 경로를 하나 선택해서 끝까지 진행하고, 실패하면 방문 표시를 되돌려 다른 경로를 시도
3+ bfs, 백트래킹
4+ * @param {character[][] } board
5+ * @param {string } word
6+ * @return {boolean }
7+ */
8+ var exist = function ( board , word ) {
9+ const rows = board . length ;
10+ const cols = board [ 0 ] . length ;
11+
12+ const dx = [ - 1 , 1 , 0 , 0 ] ;
13+ const dy = [ 0 , 0 , - 1 , 1 ] ;
14+
15+ function dfs ( x , y , index ) {
16+ if ( board [ y ] [ x ] !== word [ index ] ) {
17+ return false ;
18+ }
19+
20+ if ( index === word . length - 1 ) {
21+ return true ;
22+ }
23+
24+ // 방문 처리
25+ const original = board [ y ] [ x ] ;
26+ board [ y ] [ x ] = "#" ;
27+
28+ for ( let i = 0 ; i < 4 ; i ++ ) {
29+ const nx = x + dx [ i ] ;
30+ const ny = y + dy [ i ] ;
31+
32+ if (
33+ nx >= 0 &&
34+ nx < cols &&
35+ ny >= 0 &&
36+ ny < rows &&
37+ board [ ny ] [ nx ] !== "#"
38+ ) {
39+ if ( dfs ( nx , ny , index + 1 ) ) {
40+ board [ y ] [ x ] = original ;
41+ return true ;
42+ }
43+ }
44+ }
45+
46+ board [ y ] [ x ] = original ;
47+ return false ;
48+ }
49+
50+ for ( let i = 0 ; i < rows ; i ++ ) {
51+ for ( let j = 0 ; j < cols ; j ++ ) {
52+ if ( dfs ( j , i , 0 ) ) return true ;
53+ }
54+ }
55+ return false ;
56+ } ;
You can’t perform that action at this time.
0 commit comments