From f0cf55f2825a01fba496815c9e29e920d839b158 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Wed, 10 Jul 2024 09:19:14 -0400 Subject: [PATCH 1/5] solve : palindromic substrings --- palindromic-substrings/samthekorean.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 palindromic-substrings/samthekorean.py 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 From 3e6706add72e4e02014ea19819e9609e5d24d24e Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 12 Jul 2024 07:56:58 -0400 Subject: [PATCH 2/5] decode ways --- decode-ways/samthekorean.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 decode-ways/samthekorean.py 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] From bab86ff1869cd7fd69abff81fc97dfc87e42bf93 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 12 Jul 2024 08:05:14 -0400 Subject: [PATCH 3/5] solve : coin change --- coin-change/samthekorean.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 coin-change/samthekorean.py 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 From f407e6e2c264b69bb3f8a1d78713665766f9ed4f Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 12 Jul 2024 19:36:15 -0400 Subject: [PATCH 4/5] solve : maximum product subarray --- maximum-product-subarray/samthekorean.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 maximum-product-subarray/samthekorean.py 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 From 997903b8589d804a935c42862be99f7a249e103f Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Sat, 13 Jul 2024 17:42:23 -0400 Subject: [PATCH 5/5] solve : word break --- word-break/samthekorean.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 word-break/samthekorean.py 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]