File tree Expand file tree Collapse file tree 5 files changed +87
-0
lines changed Expand file tree Collapse file tree 5 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ # a is number of amount and c is number of coins
2+ # TC : O(a∗c)
3+ # SC: O(a)
4+ class Solution :
5+ def coinChange (self , denominations : List [int ], target_amount : int ) -> int :
6+ minimum_coins = [target_amount + 1 ] * (target_amount + 1 )
7+ minimum_coins [0 ] = 0
8+
9+ for current_amount in range (1 , target_amount + 1 ):
10+ for coin in denominations :
11+ if current_amount - coin >= 0 :
12+ minimum_coins [current_amount ] = min (
13+ minimum_coins [current_amount ],
14+ 1 + minimum_coins [current_amount - coin ],
15+ )
16+
17+ return minimum_coins [- 1 ] if minimum_coins [- 1 ] != target_amount + 1 else - 1
Original file line number Diff line number Diff line change 1+ # TC : O(n)
2+ # SC : O(n)
3+ class Solution :
4+ def numDecodings (self , s : str ) -> int :
5+ if not s or s [0 ] == "0" :
6+ return 0
7+
8+ n = len (s )
9+ dp = [0 ] * (n + 1 )
10+ dp [0 ] = 1
11+ dp [1 ] = 1
12+
13+ for i in range (2 , n + 1 ):
14+ one_digit = int (s [i - 1 ])
15+ two_digits = int (s [i - 2 : i ])
16+
17+ if one_digit != 0 :
18+ dp [i ] += dp [i - 1 ]
19+
20+ if 10 <= two_digits <= 26 :
21+ dp [i ] += dp [i - 2 ]
22+
23+ return dp [n ]
Original file line number Diff line number Diff line change 1+ # TC : O(n)
2+ # SC : O(1)
3+ class Solution :
4+ def maxProduct (self , nums : List [int ]) -> int :
5+ max_overall = nums [0 ]
6+ current_min_product = current_max_product = 1
7+
8+ for num in nums :
9+ temp_min = min (current_min_product * num , current_max_product * num , num )
10+ current_max_product = max (
11+ current_min_product * num , current_max_product * num , num
12+ )
13+ current_min_product = temp_min
14+ max_overall = max (current_max_product , max_overall )
15+
16+ return max_overall
Original file line number Diff line number Diff line change 1+ # O(n^2)
2+ # O(1)
3+ class Solution :
4+ def countSubstrings (self , s : str ) -> int :
5+ length , total_palindromes = len (s ), 0
6+
7+ def countPalindromes (left : int , right : int ) -> int :
8+ count = 0
9+ while left >= 0 and right < length and s [left ] == s [right ]:
10+ left -= 1
11+ right += 1
12+ count += 1
13+ return count
14+
15+ for i in range (length ):
16+ total_palindromes += countPalindromes (i , i + 1 ) # even length palindromes
17+ total_palindromes += countPalindromes (i , i ) # odd length palindromes
18+
19+ return total_palindromes
Original file line number Diff line number Diff line change 1+ # TC : O(s^2*w)
2+ # SC : O(s)
3+ class Solution :
4+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
5+ dp = [True ] + [False ] * len (s )
6+ for n in range (1 , len (s ) + 1 ):
7+ for word in wordDict :
8+ if s [n - len (word ) : n ] == word :
9+ dp [n ] = dp [n - len (word )]
10+ if dp [n ]:
11+ break
12+ return dp [- 1 ]
You can’t perform that action at this time.
0 commit comments