Skip to content

Commit d52bb73

Browse files
authored
Merge pull request #844 from HodaeSsi/main
[HodaeSsi] Week4
2 parents 9695d07 + 59c596a commit d52bb73

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

coin-change/HodaeSsi.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# space complexity: O(n) (여기서 n은 amount)
2+
# time complexity: O(n * m) (여기서 n은 amount, m은 coins의 길이)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def coinChange(self, coins: List[int], amount: int) -> int:
8+
dp = [float('inf')] * (amount + 1)
9+
dp[0] = 0
10+
11+
for i in range(1, amount + 1):
12+
for coin in coins:
13+
if coin <= i:
14+
dp[i] = min(dp[i], dp[i - coin] + 1)
15+
16+
return dp[amount] if dp[amount] != float('inf') else -1
17+

merge-two-sorted-lists/HodaeSsi.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
class Solution:
10+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
11+
if not list1:
12+
return list2
13+
if not list2:
14+
return list1
15+
16+
if list1.val < list2.val:
17+
list1.next = self.mergeTwoLists(list1.next, list2)
18+
return list1
19+
else:
20+
list2.next = self.mergeTwoLists(list1, list2.next)
21+
return list2
22+

missing-number/HodaeSsi.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def missingNumber(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
expected_sum = (n ** 2 + n) // 2
5+
6+
actual_sum = sum(nums)
7+
8+
return expected_sum - actual_sum
9+

palindromic-substrings/HodaeSsi.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# space complexity: O(n^2)
2+
# time complexity: O(n^2) (*exactly, n^2 / 2)
3+
class Solution:
4+
def countSubstrings(self, s: str) -> int:
5+
dp = [[False] * len(s) for _ in range(len(s))] # dp[i][j] = True if s[i:j+1] is a palindrome
6+
for i in range(len(s)):
7+
for j in range(i, -1, -1):
8+
if i == j:
9+
dp[j][i] = True
10+
continue
11+
if i - j == 1:
12+
dp[j][i] = s[i] == s[j]
13+
continue
14+
if s[i] == s[j]:
15+
if dp[j+1][i-1]:
16+
dp[j][i] = True
17+
18+
return sum([sum(row) for row in dp])
19+

word-search/HodaeSsi.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def dfs (self, board, word, visited, y, x, word_idx):
3+
if word_idx == len(word):
4+
return True
5+
6+
if y < 0 or y >= len(board) or x < 0 or x >= len(board[0]) or visited[y][x] or board[y][x] != word[word_idx]:
7+
return False
8+
9+
visited[y][x] = True
10+
for dy, dx in [(1, 0), (-1, 0), (0, -1), (0, 1)]:
11+
if self.dfs(board, word, visited, y + dy, x + dx, word_idx + 1):
12+
return True
13+
visited[y][x] = False
14+
return False
15+
16+
def exist(self, board: List[List[str]], word: str) -> bool:
17+
visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))]
18+
19+
# find fisrt letter in board
20+
for y in range(len(board)):
21+
for x in range(len(board[0])):
22+
if board[y][x] == word[0]:
23+
visited[y][x] = True
24+
for dy, dx in [(1, 0), (-1, 0), (0, -1), (0, 1)]:
25+
if self.dfs(board, word, visited, y + dy, x + dx, 1):
26+
return True
27+
visited[y][x] = False
28+
return False
29+

0 commit comments

Comments
 (0)