-
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 #806 from KwonNayeon/main
[KwonNayeon] Week 4
- Loading branch information
Showing
4 changed files
with
154 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,30 @@ | ||
""" | ||
Constraints: | ||
1. 1 <= coins.length <= 12 | ||
2. 1 <= coins[i] <= 2^31 - 1 | ||
3. 0 <= amount <= 10^4 | ||
Time Complexity: O(N*M) | ||
- N์ amount | ||
- M์ ๋์ ์ ๊ฐ์ (coins.length) | ||
Space Complexity: O(N) | ||
- N์ amount | ||
To Do: | ||
- DP ๋ฌธ์ ๋ณต์ตํ๊ธฐ | ||
- Bottom-up๊ณผ Top-down ๋ฐฉ์์ ์ฐจ์ด์ ์ดํดํ๊ธฐ | ||
- ๊ทธ๋ฆฌ๋์ DP์ ์ฐจ์ด์ ๋ณต์ตํ๊ธฐ | ||
""" | ||
|
||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
dp = [sys.maxsize // 2] * (amount + 1) | ||
dp[0] = 0 | ||
|
||
for i in range(1, amount + 1): | ||
for coin in coins: | ||
if i >= coin: | ||
dp[i] = min(dp[i], dp[i-coin] + 1) | ||
|
||
return dp[amount] if dp[amount] != sys.maxsize // 2 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,45 @@ | ||
""" | ||
Constraints: | ||
1. 0 <= list1.length, list2.length <= 50 | ||
2. -100 <= Node.val <= 100 | ||
3. list1 and list2 are sorted in non-decreasing order | ||
Time Complexity: n๊ณผ m์ด ๊ฐ๊ฐ list1๊ณผ list2์ ๊ธธ์ด๋ฅผ ๋ํ๋ผ ๋, O(n + m) | ||
- ๊ฐ ๋ ธ๋๋ฅผ ํ ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธํ๊ธฐ ๋๋ฌธ | ||
Space Complexity: O(1) | ||
- ์๋ก์ด ๋ ธ๋๋ฅผ ๋ง๋ค์ง ์๊ณ ๊ธฐ์กด ๋ ธ๋๋ค์ ์ฐ๊ฒฐ๋ง ๋ฐ๊พธ๊ธฐ ๋๋ฌธ | ||
ํ์ด ๋ฐฉ๋ฒ: | ||
1. ๋๋ฏธ ๋ ธ๋๋ฅผ ๋ง๋ค์ด์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์์์ ์ผ๋ก ์ฌ์ฉ | ||
2. ๋ ๋ฆฌ์คํธ๋ฅผ ์์์๋ถํฐ ์ํํ๋ฉด์ ๊ฐ์ ๋น๊ต | ||
3. ๋ ์์ ๊ฐ์ ๊ฐ์ง ๋ ธ๋๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ฐ๊ฒฐํ๊ณ , ํด๋น ๋ฆฌ์คํธ์ ํฌ์ธํฐ๋ฅผ ๋ค์์ผ๋ก ์ด๋ | ||
4. ํ์ชฝ ๋ฆฌ์คํธ๊ฐ ๋๋๋ฉด, ๋จ์ ๋ฆฌ์คํธ๋ฅผ ๊ทธ๋๋ก ๊ฒฐ๊ณผ ๋ฆฌ์คํธ ๋ค์ ์ฐ๊ฒฐ | ||
5. ๋๋ฏธ ๋ ธ๋์ next๋ฅผ ๋ฐํ (์ค์ ์ ๋ ฌ๋ ๋ฆฌ์คํธ์ ์์์ ) | ||
""" | ||
|
||
# Definition for singly-linked list. | ||
# 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]: | ||
result = ListNode() | ||
current = result | ||
|
||
while list1 and list2: | ||
if list1.val <= list2.val: | ||
current.next = list1 | ||
list1 = list1.next | ||
else: | ||
current.next = list2 | ||
list2 = list2.next | ||
current = current.next | ||
|
||
if list1: | ||
current.next = list1 | ||
if list2: | ||
current.next = list2 | ||
|
||
return result.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,28 @@ | ||
""" | ||
Constraints: | ||
1. n equals the length of array nums | ||
2. n is between 1 and 10^4 inclusive | ||
3. Each element nums[i] is between 0 and n inclusive | ||
4. All numbers in nums are unique (no duplicates) | ||
Time Complexity: O(nlogn) | ||
- ์ ๋ ฌ์ nlogn, ์ํ์ n์ด ํ์ํ๋ฏ๋ก ์ ์ฒด์ ์ผ๋ก O(nlogn) | ||
Space Complexity: O(1) | ||
- ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ ์ ๋ ฅ ๋ฐฐ์ด๋ง ์ฌ์ฉ | ||
ํ์ด ๋ฐฉ๋ฒ: | ||
1. ๋ฐฐ์ด์ ์ ๋ ฌํ์ฌ 0๋ถํฐ n๊น์ง ์์๋๋ก ์์ด์ผ ํ ์์น์ ์๋ ์ซ์๋ฅผ ์ฐพ์ | ||
2. ์ธ๋ฑ์ค์ ํด๋น ์์น์ ๊ฐ์ ๋น๊ตํ์ฌ ๋ค๋ฅด๋ค๋ฉด ๊ทธ ์ธ๋ฑ์ค๊ฐ missing number | ||
3. ๋ชจ๋ ์ธ๋ฑ์ค๋ฅผ ํ์ธํ๋๋ฐ๋ ์๋ค๋ฉด n์ด missing number | ||
""" | ||
|
||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
nums.sort() | ||
|
||
for i in range(len(nums)): | ||
if nums[i] != i: | ||
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,51 @@ | ||
""" | ||
Constraints: | ||
1. m equals board length (number of rows) | ||
2. n equals board[i] length (number of columns) | ||
3. m and n are between 1 and 6 inclusive | ||
4. word length is between 1 and 15 inclusive | ||
5. board and word contain only lowercase and uppercase English letters | ||
Time Complexity: O(N * 3^L) | ||
- N์ board์ ๋ชจ๋ cell (m * n) | ||
- L์ word์ ๊ธธ์ด | ||
- ๊ฐ cell์์ ์์ํ์ฌ word์ ๊ฐ ๊ธ์๋ง๋ค ์ธ๋ฐฉํฅ์ผ๋ก ํ์ (์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋ฐฉํฅ ์ ์ธ) | ||
Space Complexity: O(L) | ||
- L์ word์ ๊ธธ์ด๋ก, ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด | ||
To Do: | ||
- DFS์ ๋ฐฑํธ๋ํน ๊ฐ๋ ๋ณต์ตํ๊ธฐ | ||
- ๋ค๋ฅธ ์คํฐ๋์๋ถ๋ค์ ๋ต์ ์ฐธ์กฐํ์ฌ ๋ค๋ฅธ ํ์ด๋ฐฉ๋ฒ ๋ณต์ตํ๊ธฐ | ||
""" | ||
|
||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
rows, cols = len(board), len(board[0]) | ||
|
||
def dfs(i, j, k): | ||
if k == len(word): | ||
return True | ||
|
||
if (i < 0 or i >= rows or | ||
j < 0 or j >= cols or | ||
board[i][j] != word[k]): | ||
return False | ||
|
||
temp = board[i][j] | ||
board[i][j] = '#' | ||
|
||
result = (dfs(i+1, j, k+1) or | ||
dfs(i-1, j, k+1) or | ||
dfs(i, j+1, k+1) or | ||
dfs(i, j-1, k+1)) | ||
|
||
board[i][j] = temp | ||
return result | ||
|
||
for i in range(rows): | ||
for j in range(cols): | ||
if board[i][j] == word[0]: | ||
if dfs(i, j, 0): | ||
return True | ||
return False |