Skip to content

Commit

Permalink
Merge pull request #810 from jinah92/main
Browse files Browse the repository at this point in the history
[jinah92] Week 4
  • Loading branch information
jinah92 authored Jan 3, 2025
2 parents 694335e + e1d03cd commit df3d66f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions coin-change/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# O(C*A) times, O(A) spaces
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [amount + 1] * amount

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

return dp[amount] if dp[amount] < amount + 1 else -1
18 changes: 18 additions & 0 deletions merge-two-sorted-lists/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# O(m+n) times, O(1) spaces
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(None)
node = dummy

while list1 and list2:
if list1.val < list2.val:
node.next = list1
list1 = list1.next
else:
node.next = list2
list2 = list2.next

node = node.next

node.next = list1 or list2
return dummy.next
10 changes: 10 additions & 0 deletions missing-number/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# O(n) times, O(n) spaces
class Solution:
def missingNumber(self, nums: List[int]) -> int:
nums_keys = set(nums)

for i in range(len(nums)):
if not i in nums_keys:
return i

return len(nums)
39 changes: 39 additions & 0 deletions palindromic-substrings/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# O(N^3) times, O(1) spaces
# ๋‚ด๋ถ€ while๋ฌธ์˜ ๊ด€๊ณ„๊ฐ€ ์™ธ๋ถ€ while๋ฌธ์˜ sub_str_len์— ๋”ฐ๋ผ ๋ฐ˜๋ณตํšŸ์ˆ˜๊ฐ€ ์ค„์–ด๋“œ๋ฏ€๋กœ, 1+2+...N = N(N-1)/2 = O(N2) ์‹œ๊ฐ„ ์†Œ์š”
# ์ถ”๋ผ๊ณ  ๋‚ด๋ถ€ while์—์„œ sub_str_len์— ๋”ฐ๋ผ s๊ฐ€ ์ธ๋ฑ์‹ฑ๋˜๋ฏ€๋กœ ์ตœ๋Œ€ O(N) ์‹œ๊ฐ„์ด ์†Œ์š”
# ์ตœ์ข…์ ์œผ๋กœ O(N^2 * N) = O(N^3)์ด ์†Œ์š”๋จ
class Solution:
def countSubstrings(self, s: str) -> int:
sub_str_len = 1
result = 0

while sub_str_len <= len(s):
start_idx = 0
while start_idx + sub_str_len <= len(s):
sub_str = s[start_idx:start_idx+sub_str_len]
if sub_str == sub_str[::-1]:
result += 1
start_idx += 1

sub_str_len += 1


return result

# DP ํ’€์ด
# O(N^2) times, O(N^2) spaces
# start, end ์ง€์ ์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์ด์ „ ๊ณ„์‚ฐ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ํšŒ๋ฌธ์„ ํŒŒ์•…
class Solution2:
def countSubstrings(self, s: str) -> int:
dp = {}

for end in range(len(s)):
for start in range(end, -1, -1):
if start == end:
dp[(start, end)] = True
elif start + 1 == end:
dp[(start, end)] = s[start] == s[end]
else:
dp[(start, end)] = s[start] == s[end] and dp[(start+1, end-1)]

return list(dp.values()).count(True)
24 changes: 24 additions & 0 deletions word-search/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# O(M*N*4^W) times, O(W) spaces
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])

directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]

def dfs(row, col, idx):
if idx == len(word):
return True
if not (0 <= row < rows and 0 <= col < cols):
return False
if board[row][col] != word[idx]:
return False

temp_val = board[row][col]
board[row][col] = ""

result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions)

board[row][col] = temp_val
return result

return any(dfs(r, c, 0) for r in range(rows) for c in range(cols))

0 comments on commit df3d66f

Please sign in to comment.