Skip to content

[SAM] Week 11 solutions #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions coin-change/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions decode-ways/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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]
16 changes: 16 additions & 0 deletions maximum-product-subarray/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions palindromic-substrings/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions word-break/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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]