Skip to content

Commit b186a75

Browse files
author
sangbeenmoon
committed
week4.
1 parent 41c043e commit b186a75

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

coin-change/sangbeenmoon.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,44 @@ def go(self, x: int, coins: List[int]) -> int:
3030
self.dp[x] = mm if mm != 100000 else -1 # memoization
3131

3232
return self.dp[x]
33+
34+
35+
36+
37+
38+
39+
40+
41+
# --------
42+
43+
class Solution:
44+
def coinChange(self, coins: List[int], amount: int) -> int:
45+
memo = {}
46+
47+
48+
for coin in coins:
49+
memo[coin] = 1
50+
51+
def go(target: int) -> int:
52+
if target < 0:
53+
return -1
54+
55+
if target in memo:
56+
return memo[target]
57+
58+
min_val = 10001
59+
60+
for coin in coins:
61+
res = go(target - coin)
62+
if res >= 0:
63+
min_val = min(min_val, res)
64+
65+
memo[target] = (min_val + 1 if min_val != 10001 else -1)
66+
67+
return memo[target]
68+
69+
if amount == 0:
70+
return 0
71+
72+
return go(amount)
73+

find-minimum-in-rotated-sorted-array/sangbeenmoon.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,25 @@ def findMin(self, nums: List[int]) -> int:
2020
right = mid
2121

2222
return nums[left]
23+
24+
# -----------
25+
26+
# TC : O(logN)
27+
# SC : 1
28+
29+
class Solution:
30+
def findMin(self, nums: List[int]) -> int:
31+
32+
left = 0
33+
right = len(nums) - 1
34+
mid = (left + right) // 2
35+
36+
while left < right:
37+
mid = (left + right) // 2
38+
39+
if nums[mid] > nums[right]:
40+
left = mid + 1
41+
else:
42+
right = mid
43+
44+
return nums[left]

word-search/sangbeenmoon.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,43 @@ def dfs(self, board, word, cx, cy, idx):
3434
self.visited[ny][nx] = True
3535
self.dfs(board, word, nx, ny, idx + 1)
3636
self.visited[ny][nx] = False
37+
38+
39+
# ---
40+
41+
class Solution:
42+
def exist(self, board: List[List[str]], word: str) -> bool:
43+
44+
dx = [0,0,-1,1]
45+
dy = [-1,1,0,0]
46+
47+
x_len , y_len = len(board[0]) , len(board)
48+
49+
visited = [[False] * x_len for _ in range(y_len)]
50+
51+
def go(xx: int, yy: int, pos: int) -> bool:
52+
53+
if pos >= len(word) - 1:
54+
return True
55+
56+
for d in range(4):
57+
nx = xx + dx[d]
58+
ny = yy + dy[d]
59+
60+
if 0 <= nx and nx < x_len and 0 <= ny and ny < y_len:
61+
if not visited[ny][nx] and board[ny][nx] == word[pos + 1]:
62+
visited[ny][nx] = True
63+
if go(nx,ny,pos+1):
64+
return True
65+
visited[ny][nx] = False
66+
return False
67+
68+
for y in range(y_len):
69+
for x in range(x_len):
70+
if board[y][x] == word[0]:
71+
visited[y][x] = True
72+
if go(x,y,0):
73+
return True
74+
visited[y][x] = False
75+
76+
return False

0 commit comments

Comments
 (0)