Skip to content

[HodaeSsi] Week4 #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 5, 2025
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
17 changes: 17 additions & 0 deletions coin-change/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# space complexity: O(n) (여기서 n은 amount)
# time complexity: O(n * m) (여기서 n은 amount, m은 coins의 길이)
from typing import List


class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [float('inf')] * (amount + 1)
dp[0] = 0

for i in range(1, amount + 1):
for coin in coins:
if coin <= i:
dp[i] = min(dp[i], dp[i - coin] + 1)

return dp[amount] if dp[amount] != float('inf') else -1

22 changes: 22 additions & 0 deletions merge-two-sorted-lists/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Optional


class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list1:
return list2
if not list2:
return list1

if list1.val < list2.val:
list1.next = self.mergeTwoLists(list1.next, list2)
return list1
else:
list2.next = self.mergeTwoLists(list1, list2.next)
return list2

9 changes: 9 additions & 0 deletions missing-number/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
expected_sum = (n ** 2 + n) // 2

actual_sum = sum(nums)

return expected_sum - actual_sum

19 changes: 19 additions & 0 deletions palindromic-substrings/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# space complexity: O(n^2)
# time complexity: O(n^2) (*exactly, n^2 / 2)
class Solution:
def countSubstrings(self, s: str) -> int:
dp = [[False] * len(s) for _ in range(len(s))] # dp[i][j] = True if s[i:j+1] is a palindrome
for i in range(len(s)):
for j in range(i, -1, -1):
if i == j:
dp[j][i] = True
continue
if i - j == 1:
dp[j][i] = s[i] == s[j]
continue
if s[i] == s[j]:
if dp[j+1][i-1]:
dp[j][i] = True

return sum([sum(row) for row in dp])

29 changes: 29 additions & 0 deletions word-search/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:
def dfs (self, board, word, visited, y, x, word_idx):
if word_idx == len(word):
return True

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]:
return False

visited[y][x] = True
for dy, dx in [(1, 0), (-1, 0), (0, -1), (0, 1)]:
if self.dfs(board, word, visited, y + dy, x + dx, word_idx + 1):
return True
visited[y][x] = False
return False

def exist(self, board: List[List[str]], word: str) -> bool:
visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))]

# find fisrt letter in board
for y in range(len(board)):
for x in range(len(board[0])):
if board[y][x] == word[0]:
visited[y][x] = True
for dy, dx in [(1, 0), (-1, 0), (0, -1), (0, 1)]:
if self.dfs(board, word, visited, y + dy, x + dx, 1):
return True
visited[y][x] = False
return False

Loading