Skip to content

Commit 6489b9b

Browse files
committed
feat: week4 medium 문제풀이
1 parent c1d6281 commit 6489b9b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

coin-change/jinah92.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# O(C*A) times, O(A) spaces
2+
class Solution:
3+
def coinChange(self, coins: List[int], amount: int) -> int:
4+
dp = [0] + [amount + 1] * amount
5+
6+
for coin in coins:
7+
for i in range(coin, amount + 1):
8+
dp[i] = min(dp[i], dp[i-coin]+1)
9+
10+
return dp[amount] if dp[amount] < amount + 1 else -1

palindromic-substrings/jinah92.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# O((LogN)^N) times, O(1) spaces
2+
class Solution:
3+
def countSubstrings(self, s: str) -> int:
4+
sub_str_len = 1
5+
result = 0
6+
7+
while sub_str_len <= len(s):
8+
start_idx = 0
9+
while start_idx + sub_str_len <= len(s):
10+
sub_str = s[start_idx:start_idx+sub_str_len]
11+
if sub_str == sub_str[::-1]:
12+
result += 1
13+
start_idx += 1
14+
15+
sub_str_len += 1
16+
17+
18+
return result

word-search/jinah92.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# O(M*N*4^W) times, O(M*N+W) spaces
2+
class Solution:
3+
def exist(self, board: List[List[str]], word: str) -> bool:
4+
rows, cols = len(board), len(board[0])
5+
visited = set()
6+
7+
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
8+
9+
def dfs(row, col, idx):
10+
if idx == len(word):
11+
return True
12+
if not (0 <= row < rows and 0 <= col < cols):
13+
return False
14+
if board[row][col] != word[idx]:
15+
return False
16+
if (row, col) in visited:
17+
return False
18+
19+
visited.add((row, col))
20+
result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions)
21+
visited.remove((row, col))
22+
23+
return result
24+
25+
return any(dfs(r, c, 0) for r in range(rows) for c in range(cols))

0 commit comments

Comments
 (0)