Skip to content

Commit df3d66f

Browse files
authored
Merge pull request #810 from jinah92/main
[jinah92] Week 4
2 parents 694335e + e1d03cd commit df3d66f

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

coin-change/jinah92.py

+10
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

merge-two-sorted-lists/jinah92.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# O(m+n) times, O(1) spaces
2+
class Solution:
3+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
4+
dummy = ListNode(None)
5+
node = dummy
6+
7+
while list1 and list2:
8+
if list1.val < list2.val:
9+
node.next = list1
10+
list1 = list1.next
11+
else:
12+
node.next = list2
13+
list2 = list2.next
14+
15+
node = node.next
16+
17+
node.next = list1 or list2
18+
return dummy.next

missing-number/jinah92.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# O(n) times, O(n) spaces
2+
class Solution:
3+
def missingNumber(self, nums: List[int]) -> int:
4+
nums_keys = set(nums)
5+
6+
for i in range(len(nums)):
7+
if not i in nums_keys:
8+
return i
9+
10+
return len(nums)

palindromic-substrings/jinah92.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# O(N^3) times, O(1) spaces
2+
# 내부 while문의 관계가 외부 while문의 sub_str_len에 따라 반복횟수가 줄어드므로, 1+2+...N = N(N-1)/2 = O(N2) 시간 소요
3+
# 추라고 내부 while에서 sub_str_len에 따라 s가 인덱싱되므로 최대 O(N) 시간이 소요
4+
# 최종적으로 O(N^2 * N) = O(N^3)이 소요됨
5+
class Solution:
6+
def countSubstrings(self, s: str) -> int:
7+
sub_str_len = 1
8+
result = 0
9+
10+
while sub_str_len <= len(s):
11+
start_idx = 0
12+
while start_idx + sub_str_len <= len(s):
13+
sub_str = s[start_idx:start_idx+sub_str_len]
14+
if sub_str == sub_str[::-1]:
15+
result += 1
16+
start_idx += 1
17+
18+
sub_str_len += 1
19+
20+
21+
return result
22+
23+
# DP 풀이
24+
# O(N^2) times, O(N^2) spaces
25+
# start, end 지점을 순회하면서 이전 계산값을 재사용하여 회문을 파악
26+
class Solution2:
27+
def countSubstrings(self, s: str) -> int:
28+
dp = {}
29+
30+
for end in range(len(s)):
31+
for start in range(end, -1, -1):
32+
if start == end:
33+
dp[(start, end)] = True
34+
elif start + 1 == end:
35+
dp[(start, end)] = s[start] == s[end]
36+
else:
37+
dp[(start, end)] = s[start] == s[end] and dp[(start+1, end-1)]
38+
39+
return list(dp.values()).count(True)

word-search/jinah92.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# O(M*N*4^W) times, O(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+
6+
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
7+
8+
def dfs(row, col, idx):
9+
if idx == len(word):
10+
return True
11+
if not (0 <= row < rows and 0 <= col < cols):
12+
return False
13+
if board[row][col] != word[idx]:
14+
return False
15+
16+
temp_val = board[row][col]
17+
board[row][col] = ""
18+
19+
result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions)
20+
21+
board[row][col] = temp_val
22+
return result
23+
24+
return any(dfs(r, c, 0) for r in range(rows) for c in range(cols))

0 commit comments

Comments
 (0)