File tree Expand file tree Collapse file tree 5 files changed +179
-0
lines changed Expand file tree Collapse file tree 5 files changed +179
-0
lines changed Original file line number Diff line number Diff line change
1
+ // n: amount m: length of coins
2
+ // Time complexity: O(n * m) + O(mlogm)
3
+ // Space complexity: O(n)
4
+
5
+ /**
6
+ * @param {number[] } coins
7
+ * @param {number } amount
8
+ * @return {number }
9
+ */
10
+ var coinChange = function ( coins , amount ) {
11
+ const dp = Array . from ( { length : amount + 1 } , ( ) => Infinity ) ;
12
+ dp [ 0 ] = 0 ;
13
+
14
+ coins . sort ( ( a , b ) => a - b ) ;
15
+
16
+ for ( const coin of coins ) {
17
+ for ( let i = coin ; i <= amount ; i ++ ) {
18
+ dp [ i ] = Math . min ( dp [ i ] , dp [ i - coin ] + 1 ) ;
19
+ }
20
+ }
21
+
22
+ return dp . at ( - 1 ) === Infinity ? - 1 : dp . at ( - 1 ) ;
23
+ } ;
Original file line number Diff line number Diff line change
1
+ // m: list1, n: list2
2
+ // Time complexity: O(m+n)
3
+ // Space complexity: O(m+n)
4
+
5
+ /**
6
+ * Definition for singly-linked list.
7
+ * function ListNode(val, next) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.next = (next===undefined ? null : next)
10
+ * }
11
+ */
12
+ /**
13
+ * @param {ListNode } list1
14
+ * @param {ListNode } list2
15
+ * @return {ListNode }
16
+ */
17
+ var mergeTwoLists = function ( list1 , list2 ) {
18
+ const answer = new ListNode ( ) ;
19
+ let current = answer ;
20
+
21
+ while ( list1 && list2 ) {
22
+ if ( list1 . val < list2 . val ) {
23
+ current . next = list1 ;
24
+ list1 = list1 . next ;
25
+ } else {
26
+ current . next = list2 ;
27
+ list2 = list2 . next ;
28
+ }
29
+
30
+ current = current . next ;
31
+ }
32
+
33
+ if ( list1 ) {
34
+ current . next = list1 ;
35
+ }
36
+
37
+ if ( list2 ) {
38
+ current . next = list2 ;
39
+ }
40
+
41
+ return answer . next ;
42
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time complexity: O(n)
2
+ // Space complexity: O(1)
3
+
4
+ /**
5
+ * @param {number[] } nums
6
+ * @return {number }
7
+ */
8
+ var missingNumber = function ( nums ) {
9
+ const n = nums . length ;
10
+ const target = ( n * ( n + 1 ) ) / 2 ;
11
+
12
+ const sum = nums . reduce ( ( a , c ) => a + c , 0 ) ;
13
+
14
+ return target - sum ;
15
+ } ;
Original file line number Diff line number Diff line change
1
+ // Time complexity: O(n^2)
2
+ // Space complexity: O(n^2)
3
+
4
+ /**
5
+ * @param {string } s
6
+ * @return {number }
7
+ */
8
+ var countSubstrings = function ( s ) {
9
+ const n = s . length ;
10
+ const dp = Array . from ( { length : n } , ( ) =>
11
+ Array . from ( { length : n } , ( ) => false )
12
+ ) ;
13
+ let answer = 0 ;
14
+
15
+ for ( let end = 0 ; end < n ; end ++ ) {
16
+ for ( let start = end ; start >= 0 ; start -- ) {
17
+ if ( start === end ) {
18
+ dp [ start ] [ end ] = true ;
19
+ answer ++ ;
20
+ continue ;
21
+ }
22
+
23
+ if ( start + 1 === end ) {
24
+ if ( s [ start ] === s [ end ] ) {
25
+ dp [ start ] [ end ] = true ;
26
+ answer ++ ;
27
+ }
28
+ continue ;
29
+ }
30
+
31
+ if ( s [ start ] === s [ end ] && dp [ start + 1 ] [ end - 1 ] ) {
32
+ dp [ start ] [ end ] = true ;
33
+ answer ++ ;
34
+ continue ;
35
+ }
36
+ }
37
+ }
38
+
39
+ return answer ;
40
+ } ;
Original file line number Diff line number Diff line change
1
+ // h: height of the board, w: width of the board, n: length of the word
2
+ // Time complexity: O(h * w * 4**n)
3
+ // Space complexity: O(n)
4
+
5
+ /**
6
+ * @param {character[][] } board
7
+ * @param {string } word
8
+ * @return {boolean }
9
+ */
10
+ var exist = function ( board , word ) {
11
+ const n = word . length ;
12
+ const h = board . length ;
13
+ const w = board [ 0 ] . length ;
14
+
15
+ const dy = [ 1 , 0 , - 1 , 0 ] ;
16
+ const dx = [ 0 , 1 , 0 , - 1 ] ;
17
+
18
+ let answer = false ;
19
+
20
+ const dfs = ( current , index ) => {
21
+ if ( index === n - 1 ) {
22
+ answer = true ;
23
+ return ;
24
+ }
25
+
26
+ const [ cy , cx ] = current ;
27
+ const value = board [ cy ] [ cx ] ;
28
+ board [ cy ] [ cx ] = "" ;
29
+
30
+ for ( let i = 0 ; i < dy . length ; i ++ ) {
31
+ const ny = cy + dy [ i ] ;
32
+ const nx = cx + dx [ i ] ;
33
+ const ni = index + 1 ;
34
+
35
+ if (
36
+ ny >= 0 &&
37
+ ny < h &&
38
+ nx >= 0 &&
39
+ nx < w &&
40
+ board [ ny ] [ nx ] &&
41
+ word [ ni ] === board [ ny ] [ nx ]
42
+ ) {
43
+ dfs ( [ ny , nx ] , ni ) ;
44
+ }
45
+ }
46
+
47
+ board [ cy ] [ cx ] = value ;
48
+ } ;
49
+
50
+ for ( let i = 0 ; i < h ; i ++ ) {
51
+ for ( let j = 0 ; j < w ; j ++ ) {
52
+ if ( board [ i ] [ j ] === word [ 0 ] && ! answer ) {
53
+ dfs ( [ i , j ] , 0 ) ;
54
+ }
55
+ }
56
+ }
57
+
58
+ return answer ;
59
+ } ;
You can’t perform that action at this time.
0 commit comments