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
10 changes: 10 additions & 0 deletions coin-change/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
# O(n*m), n = len(coins), m = amount
def coinChange(self, coins: list[int], amount: int) -> int:
dp = [amount+1] * (amount+1)
dp[0] = 0
for i in range(amount+1):
for coin in coins:
if i - coin >= 0:
dp[i] = min(dp[i], dp[i-coin]+1)
return dp[amount] if dp[amount] != amount+1 else -1
17 changes: 17 additions & 0 deletions merge-two-sorted-lists/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
# O(n+m), n = len(list1), m = len(list2)
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
head = node = ListNode()
while list1 and list2:
if list1.val <= list2.val:
node.next = list1
list1 = list1.next
else:
node.next = list2
list2 = list2.next
node = node.next
if list1:
node.next = list1
if list2:
node.next = list2
return head.next
5 changes: 5 additions & 0 deletions missing-number/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
# O(n), n = len(nums)
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
return n*(n+1)//2 - sum(nums)
29 changes: 29 additions & 0 deletions word-search/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:
# O(m*n), m*n = board's width,heigh
def exist(self, board: List[List[str]], word: str) -> bool:
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
result = False

def dfs(i, j, visited, w):
nonlocal result
if (i, j) in visited:
return
if not (0 <= i < len(board)) or not (0 <= j < len(board[0])):
return
w += board[i][j]
if not (word[:len(w)] == w):
return
visited.add((i, j))
if w == word:
result = True
return
for d in dirs:
dfs(i + d[0], j + d[1], visited, w)
visited.remove((i, j)) # backtracking

for i in range(len(board)):
for j in range(len(board[0])):
dfs(i, j, set(), "")
if result:
return True
return False
Loading