-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #810 from jinah92/main
[jinah92] Week 4
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |