Skip to content
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
16 changes: 16 additions & 0 deletions coin-change/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
result = [amount + 1] * (amount + 1)
result[0] = 0

for i in range(1, amount + 1):
for c in coins:
if i >= c:
result[i] = min(result[i], result[i - c] + 1)

if result[amount] == amount + 1:
return -1

return result[amount]

## TC: O(n * len(coins)) , SC: O(n), where n denotes amount
22 changes: 22 additions & 0 deletions decode-ways/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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], dp[1] = 1, 1

for i in range(2, n + 1):
one = int(s[i - 1])
two = int(s[i - 2:i])

if 1 <= one <= 9:
dp[i] += dp[i - 1]

if 10 <= two <= 26:
dp[i] += dp[i - 2]

return dp[n]

## TC: O(n), SC: O(1)
14 changes: 14 additions & 0 deletions maximum-product-subarray/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def maxProduct(self, nums: List[int]) -> int:
curMax, curMin = 1, 1
res = nums[0]

for n in nums:
vals = (n, n * curMax, n * curMin)
curMax, curMin = max(vals), min(vals)

res = max(res, curMax)

return res

## TC: O(n), SC: O(1)
19 changes: 19 additions & 0 deletions palindromic-substrings/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def countSubstrings(self, s: str) -> int:
result = 0

def helper(left, right):
count = 0
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
return count

for i in range(len(s)):
result += helper(i, i)
result += helper(i, i + 1)

return result

## TC: O(n^2), SC: O(1)
13 changes: 13 additions & 0 deletions word-break/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
dp = [True] + [False] * len(s)

for i in range(1, len(s) + 1):
for w in wordDict:
if i - len(w) >= 0 and dp[i - len(w)] and s[:i].endswith(w):
dp[i] = True

return dp[-1]

## TC: O(len(str) * len(wordDict) * len(avg(wordDict[n])))
## SC: O(n)