File tree Expand file tree Collapse file tree 5 files changed +143
-0
lines changed
find-minimum-in-rotated-sorted-array
maximum-depth-of-binary-tree Expand file tree Collapse file tree 5 files changed +143
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } coins
3+ * @param {number } amount
4+ * @return {number }
5+ */
6+ var coinChange = function ( coins , amount ) {
7+ const dp = new Array ( amount + 1 ) . fill ( amount + 1 ) ;
8+
9+ dp [ 0 ] = 0 ;
10+
11+ for ( let i = 1 ; i <= amount ; i ++ ) {
12+ for ( let coin of coins ) {
13+ if ( i >= coin ) {
14+ dp [ i ] = Math . min ( dp [ i ] , dp [ i - coin ] + 1 ) ;
15+ }
16+ }
17+ }
18+
19+ return dp [ amount ] > amount ? - 1 : dp [ amount ] ;
20+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var findMin = function ( nums ) {
6+ const length = nums . length ;
7+
8+ let left = 0 ;
9+ let right = length - 1 ;
10+ let result = Infinity ;
11+
12+ while ( left <= right ) {
13+ let mid = Math . floor ( ( left + right ) / 2 ) ;
14+
15+ if ( nums [ mid ] < nums [ left ] ) {
16+ result = Math . min ( result , nums [ mid ] ) ;
17+ right = mid - 1 ;
18+ } else {
19+ result = Math . min ( result , nums [ left ] ) ;
20+ left = mid + 1 ;
21+ }
22+ }
23+
24+ return result ;
25+ } ;
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 } root
11+ * @return {number }
12+ */
13+ var maxDepth = function ( root ) {
14+ if ( ! root ) return 0 ;
15+
16+ const leftDepth = maxDepth ( root . left ) ;
17+ const rightDepth = maxDepth ( root . right ) ;
18+
19+ return Math . max ( leftDepth , rightDepth ) + 1 ;
20+ } ;
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+ * @param {ListNode } list1
10+ * @param {ListNode } list2
11+ * @return {ListNode }
12+ */
13+ var mergeTwoLists = function ( list1 , list2 ) {
14+ let dummy = new ListNode ( 0 ) ;
15+ let currentNode = dummy ;
16+
17+ while ( list1 && list2 ) {
18+ if ( list1 . val < list2 . val ) {
19+ currentNode . next = list1 ;
20+ list1 = list1 . next ;
21+ } else {
22+ currentNode . next = list2 ;
23+ list2 = list2 . next ;
24+ }
25+ currentNode = currentNode . next ;
26+ }
27+
28+ if ( list1 ) currentNode . next = list1 ;
29+ if ( list2 ) currentNode . next = list2 ;
30+
31+ return dummy . next ;
32+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {character[][] } board
3+ * @param {string } word
4+ * @return {boolean }
5+ */
6+ var exist = function ( board , word ) {
7+ const m = board . length ;
8+ const n = board [ 0 ] . length ;
9+
10+ const dfs = ( row , col , index ) => {
11+ if ( index === word . length ) return true ;
12+
13+ if (
14+ row < 0 ||
15+ row >= m ||
16+ col < 0 ||
17+ col >= n ||
18+ board [ row ] [ col ] !== word [ index ]
19+ ) {
20+ return false ;
21+ }
22+
23+ const temp = board [ row ] [ col ] ;
24+ board [ row ] [ col ] = "#" ;
25+
26+ const found =
27+ dfs ( row + 1 , col , index + 1 ) ||
28+ dfs ( row - 1 , col , index + 1 ) ||
29+ dfs ( row , col + 1 , index + 1 ) ||
30+ dfs ( row , col - 1 , index + 1 ) ;
31+
32+ board [ row ] [ col ] = temp ;
33+
34+ return found ;
35+ } ;
36+
37+ for ( let r = 0 ; r < m ; r ++ ) {
38+ for ( let c = 0 ; c < n ; c ++ ) {
39+ if ( board [ r ] [ c ] === word [ 0 ] && dfs ( r , c , 1 ) ) {
40+ return true ;
41+ }
42+ }
43+ }
44+
45+ return false ;
46+ } ;
You can’t perform that action at this time.
0 commit comments