diff --git a/coin-change/samthekorean.py b/coin-change/samthekorean.py new file mode 100644 index 000000000..504365b1a --- /dev/null +++ b/coin-change/samthekorean.py @@ -0,0 +1,17 @@ +# a is number of amount and c is number of coins +# TC : O(a∗c) +# SC: O(a) +class Solution: + def coinChange(self, denominations: List[int], target_amount: int) -> int: + minimum_coins = [target_amount + 1] * (target_amount + 1) + minimum_coins[0] = 0 + + for current_amount in range(1, target_amount + 1): + for coin in denominations: + if current_amount - coin >= 0: + minimum_coins[current_amount] = min( + minimum_coins[current_amount], + 1 + minimum_coins[current_amount - coin], + ) + + return minimum_coins[-1] if minimum_coins[-1] != target_amount + 1 else -1 diff --git a/decode-ways/samthekorean.py b/decode-ways/samthekorean.py new file mode 100644 index 000000000..2e05dc3dc --- /dev/null +++ b/decode-ways/samthekorean.py @@ -0,0 +1,23 @@ +# TC : O(n) +# SC : O(n) +class Solution: + def numDecodings(self, s: str) -> int: + if not s or s[0] == "0": + return 0 + + n = len(s) + dp = [0] * (n + 1) + dp[0] = 1 + dp[1] = 1 + + for i in range(2, n + 1): + one_digit = int(s[i - 1]) + two_digits = int(s[i - 2 : i]) + + if one_digit != 0: + dp[i] += dp[i - 1] + + if 10 <= two_digits <= 26: + dp[i] += dp[i - 2] + + return dp[n] diff --git a/maximum-product-subarray/samthekorean.py b/maximum-product-subarray/samthekorean.py new file mode 100644 index 000000000..4354358c3 --- /dev/null +++ b/maximum-product-subarray/samthekorean.py @@ -0,0 +1,16 @@ +# TC : O(n) +# SC : O(1) +class Solution: + def maxProduct(self, nums: List[int]) -> int: + max_overall = nums[0] + current_min_product = current_max_product = 1 + + for num in nums: + temp_min = min(current_min_product * num, current_max_product * num, num) + current_max_product = max( + current_min_product * num, current_max_product * num, num + ) + current_min_product = temp_min + max_overall = max(current_max_product, max_overall) + + return max_overall diff --git a/palindromic-substrings/samthekorean.py b/palindromic-substrings/samthekorean.py new file mode 100644 index 000000000..efdfc6c22 --- /dev/null +++ b/palindromic-substrings/samthekorean.py @@ -0,0 +1,19 @@ +# O(n^2) +# O(1) +class Solution: + def countSubstrings(self, s: str) -> int: + length, total_palindromes = len(s), 0 + + def countPalindromes(left: int, right: int) -> int: + count = 0 + while left >= 0 and right < length and s[left] == s[right]: + left -= 1 + right += 1 + count += 1 + return count + + for i in range(length): + total_palindromes += countPalindromes(i, i + 1) # even length palindromes + total_palindromes += countPalindromes(i, i) # odd length palindromes + + return total_palindromes diff --git a/word-break/samthekorean.py b/word-break/samthekorean.py new file mode 100644 index 000000000..1c231bafc --- /dev/null +++ b/word-break/samthekorean.py @@ -0,0 +1,12 @@ +# TC : O(s^2*w) +# SC : O(s) +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [True] + [False] * len(s) + for n in range(1, len(s) + 1): + for word in wordDict: + if s[n - len(word) : n] == word: + dp[n] = dp[n - len(word)] + if dp[n]: + break + return dp[-1]