Skip to content

Commit ade084a

Browse files
committed
Week 4
1 parent ee76422 commit ade084a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

coin-change/8804who.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
dp = [1e9] * (amount+1)
4+
dp[0] = 0
5+
6+
for i in range(1, amount+1):
7+
for coin in coins:
8+
if i-coin>=0:
9+
if dp[i-coin]+1<dp[i]:
10+
dp[i]=dp[i-coin]+1
11+
12+
return -1 if dp[amount]==1e9 else dp[amount]
13+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
left = 0
4+
right = len(nums)-1
5+
while True:
6+
mid = (left+right)//2
7+
if nums[left] > nums[right]:
8+
if nums[left] > nums[mid]:
9+
right = mid
10+
else:
11+
left = mid+1
12+
else:
13+
return nums[left]
14+

word-search/8804who.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
moves = [[-1, 0], [1, 0], [0, -1], [0, 1]]
4+
max_y, max_x = len(board)-1, len(board[0])-1
5+
6+
visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))]
7+
8+
9+
def dfs(y, x, idx):
10+
if idx == len(word):
11+
return True
12+
for move in moves:
13+
moved_y, moved_x = y+move[0], x+move[1]
14+
if max_y >= moved_y and moved_y >= 0 and max_x >= moved_x and moved_x >= 0:
15+
if board[moved_y][moved_x] == word[idx] and not visited[moved_y][moved_x]:
16+
visited[moved_y][moved_x] = True
17+
if dfs(moved_y, moved_x, idx+1):
18+
return True
19+
visited[moved_y][moved_x] = False
20+
21+
22+
for y in range(max_y+1):
23+
for x in range(max_x+1):
24+
if board[y][x] == word[0]:
25+
visited[y][x] = True
26+
if dfs(y, x, 1):
27+
return True
28+
visited[y][x] = False
29+
30+
return False
31+

0 commit comments

Comments
 (0)