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+ * @param {number[] } coins
3+ * @param {number } amount
4+ * @return {number }
5+ *
6+ * 문제: https://leetcode.com/problems/coin-change/
7+ * 요구사항: 동전과 총 금액이 주어질 때 해당 금액을 만드는데 최소 동전 개수를 반환
8+ */
9+ const coinChange = ( coins , amount ) => {
10+ let dp = new Array ( amount + 1 ) . fill ( amount + 1 ) ;
11+ dp [ 0 ] = 0 ;
12+ for ( let i = 1 ; i <= amount ; i ++ ) {
13+ for ( const coin of coins ) {
14+ if ( i - coin >= 0 ) {
15+ dp [ i ] = Math . min ( dp [ i ] , dp [ i - coin ] + 1 ) ;
16+ }
17+ }
18+ }
19+
20+ return dp [ amount ] > amount ? - 1 : dp [ amount ] ;
21+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ *
5+ * 문제: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
6+ * 요구사항: 이진트리 활용
7+ */
8+ const findMin = ( 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+
15+ if ( nums [ mid ] > nums [ right ] ) {
16+ left = mid + 1 ;
17+ } else {
18+ right = mid ;
19+ }
20+ }
21+
22+ return nums [ left ] ;
23+ } ;
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+ * 문제: https://leetcode.com/problems/maximum-depth-of-binary-tree/
10+ * 요구사항: 이진 트리의 최대 깊이를 반환하라.
11+ */
12+ /**
13+ * @param {TreeNode } root
14+ * @return {number }
15+ */
16+ const maxDepth = ( root ) => {
17+ if ( ! root ) return 0 ;
18+
19+ let queue = [ root ] ;
20+ let depth = 0 ;
21+
22+ while ( queue . length ) {
23+ let size = queue . length ;
24+
25+ for ( let i = 0 ; i < size ; i ++ ) {
26+ let node = queue . shift ( ) ;
27+
28+ if ( node . left ) queue . push ( node . left ) ;
29+ if ( node . right ) queue . push ( node . right ) ;
30+ }
31+ depth ++ ;
32+ }
33+ return depth ;
34+ } ;
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+ * 문제: https://leetcode.com/problems/merge-two-sorted-lists/
9+ * 요구사항: 정의된 ListNode 를 활용해서 정렬된 배열을 리턴
10+ */
11+ /**
12+ * @param {ListNode } list1
13+ * @param {ListNode } list2
14+ * @return {ListNode }
15+ */
16+
17+
18+ const mergeTwoLists = ( list1 , list2 ) => {
19+ const dummy = new ListNode ( 0 ) ;
20+ let cur = dummy ;
21+
22+ while ( list1 && list2 ) {
23+ if ( list1 . val <= list2 . val ) {
24+ cur . next = list1 ;
25+ list1 = list1 . next ;
26+ } else {
27+ cur . next = list2 ;
28+ list2 = list2 . next ;
29+ }
30+ cur = cur . next ;
31+ }
32+
33+ cur . next = list1 ?? list2 ;
34+ return dummy . next ;
35+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {character[][] } board
3+ * @param {string } word
4+ * @return {boolean }
5+ *
6+ * 문제: https://leetcode.com/problems/word-search/
7+ * 요구사항: 그리드 구조에서 word의 문자열 존재 여부에 따라 true/false를 리턴한다.
8+ * 백트래킹, dfs
9+ */
10+ const exist = ( board , word ) => {
11+ let rows = board . length ;
12+ let cols = board [ 0 ] . length ;
13+
14+ const dfs = ( r , c , index ) => {
15+ if ( index === word . length ) return true ;
16+
17+ if ( r < 0 || r >= rows || c < 0 || c >= cols || board [ r ] [ c ] !== word [ index ] ) {
18+ return false ;
19+ }
20+
21+ let temp = board [ r ] [ c ] ;
22+ board [ r ] [ c ] = '#' ;
23+
24+ let found = dfs ( r + 1 , c , index + 1 ) ||
25+ dfs ( r - 1 , c , index + 1 ) ||
26+ dfs ( r , c + 1 , index + 1 ) ||
27+ dfs ( r , c - 1 , index + 1 ) ;
28+
29+ board [ r ] [ c ] = temp ;
30+
31+ return found ;
32+ }
33+
34+ for ( let i = 0 ; i < rows ; i ++ ) {
35+ for ( let j = 0 ; j < cols ; j ++ ) {
36+ if ( board [ i ] [ j ] === word [ 0 ] && dfs ( i , j , 0 ) ) return true ;
37+ }
38+ }
39+
40+ return false ;
41+ } ;
You can’t perform that action at this time.
0 commit comments