File tree Expand file tree Collapse file tree 5 files changed +111
-0
lines changed Expand file tree Collapse file tree 5 files changed +111
-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+ dp [ 0 ] = 0 ;
9+
10+ for ( let i = 0 ; i <= amount ; i ++ ) {
11+ for ( let coin of coins ) {
12+ if ( coin <= i ) dp [ i ] = Math . min ( dp [ i - coin ] + 1 , dp [ i ] ) ;
13+ }
14+ }
15+
16+ return dp [ amount ] < amount + 1 ? dp [ amount ] : - 1 ;
17+ } ;
18+
19+ // TC: O(amount*coins.length)
20+ // SC: O(amount)
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {number }
4+ */
5+ var numDecodings = function ( s ) {
6+ // Edge case
7+ if ( s . length === 0 || s [ 0 ] === "0" ) return 0 ;
8+
9+ let dp = new Array ( s . length + 1 ) . fill ( 0 ) ;
10+
11+ dp [ 0 ] = 1 ;
12+ dp [ 1 ] = 1 ;
13+
14+ for ( let i = 2 ; i <= s . length ; i ++ ) {
15+ let single = s [ i - 1 ] ;
16+ let double = s [ i - 2 ] + s [ i - 1 ] ;
17+
18+ if ( single >= 1 && single <= 9 ) dp [ i ] += dp [ i - 1 ] ;
19+ if ( double >= 10 && double <= 26 ) dp [ i ] += dp [ i - 2 ] ;
20+ }
21+
22+ return dp [ s . length ] ;
23+ } ;
24+
25+ // TC: O(n)
26+ // SC: O(n)
Original file line number Diff line number Diff line change 1+ var maxProduct = function ( nums ) {
2+ let result = nums [ 0 ] ;
3+ let max = 1 ,
4+ min = 1 ;
5+
6+ for ( let num of nums ) {
7+ const candidates = [ min * num , max * num , num ] ;
8+ min = Math . min ( ...candidates ) ;
9+ max = Math . max ( ...candidates ) ;
10+ result = Math . max ( max , result ) ;
11+ }
12+ return result ;
13+ } ;
14+
15+ // TC: O(n)
16+ // SC: O(1)
Original file line number Diff line number Diff line change 1+ var countSubstrings = function ( s ) {
2+ let count = 0 ;
3+
4+ const isPalindrom = ( start , end ) => {
5+ while ( start >= 0 && end < s . length && s [ start ] === s [ end ] ) {
6+ start -- ;
7+ end ++ ;
8+ count ++ ;
9+ }
10+ } ;
11+
12+ for ( let i = 0 ; i < s . length ; i ++ ) {
13+ isPalindrom ( i , i ) ; // odd number of str
14+ isPalindrom ( i , i + 1 ) ; // even number of str
15+ }
16+ return count ;
17+ } ;
18+
19+ // TC: O(n^2)
20+ // SC: O(1)
Original file line number Diff line number Diff line change 1+ var wordBreak = function ( s , wordDict ) {
2+ let memo = { } ;
3+
4+ const dfs = ( start ) => {
5+ if ( start in memo ) return memo [ start ] ;
6+
7+ if ( start === s . length ) {
8+ memo [ start ] = true ;
9+ return true ;
10+ }
11+
12+ for ( let word of wordDict ) {
13+ if ( s . substring ( start , start + word . length ) === word ) {
14+ if ( dfs ( start + word . length ) ) {
15+ memo [ start ] = true ;
16+ return true ;
17+ }
18+ }
19+ }
20+ memo [ start ] = false ;
21+ return false ;
22+ } ;
23+
24+ return dfs ( 0 ) ;
25+ } ;
26+
27+ // n = s.length | m = wordDict.length | L = maximum length of word in wordDict
28+ // TC: O(n*m*L)
29+ // SC: O(n)
You can’t perform that action at this time.
0 commit comments