Repository navigation
[wlghsp] WEEK 04 Solutions #2973
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
Open
wlghsp
wants to merge
5
commits into
DaleStudy:main
Choose a base branch
from
wlghsp:week-04
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| """ | ||
| N: amount | ||
| M: coins 개수 | ||
| TC: O(N * M) | ||
| SC: O(N) | ||
|
|
||
| """ | ||
| 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 i - coin >= 0: | ||
| dp[i] = min(dp[i], dp[i - coin] + 1) | ||
|
|
||
| return dp[amount] if dp[amount] != float('inf') else - 1 |
This file contains hidden or 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,25 @@ | ||
| """ | ||
| TC: O(log N) | ||
| SC: O(1) | ||
|
|
||
| N: 배열 nums의 원소 개수 | ||
|
|
||
| - 매 반복마다 탐색 범위를 절반으로 줄이므로 O(log N) | ||
| - 별도의 자료구조나 재귀 호출을 사용하지 않으므로 O(1) | ||
| """ | ||
| class Solution: | ||
| def findMin(self, nums: list[int]) -> int: | ||
| left, right = 0, len(nums) - 1 | ||
|
|
||
| while left < right: | ||
| mid = (left + right) // 2 | ||
|
|
||
| if nums[mid] > nums[right]: | ||
| left = mid + 1 | ||
|
|
||
| else: | ||
| right = mid | ||
|
|
||
| return nums[left] | ||
|
|
||
|
|
This file contains hidden or 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,21 @@ | ||
| """ | ||
| N: 트리의 전체 노드 개수 | ||
| H: 트리의 높이 | ||
| TC : O(N) | ||
| - DFS를 통해 모든 노드를 한 번씩 방문한다 | ||
| - 각 노드에서 수행하는 연산은 O(1)이다. | ||
| - 따라서 전체 시간 복잡도는 O(N)이다. | ||
| SC: O(H) (최악의 경우 O(N)) | ||
| - 별도의 자료구조를 생성하지 않지만, 재귀 호출로 인해 Call Stack(호출 스택) 공간이 필요하다 | ||
|
|
||
| """ | ||
| class Solution: | ||
| def maxDepth(self, root: TreeNode | None) -> int: | ||
| if not root: | ||
| return 0 | ||
| left = self.maxDepth(root.left) | ||
|
|
||
| right = self.maxDepth(root.right) | ||
|
|
||
| return max(left, right) + 1 | ||
|
|
This file contains hidden or 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 @@ | ||
| """ | ||
| N: list1의 노드 개수 | ||
| M: list2의 노드 개수 | ||
|
|
||
| TC : O(N + M) | ||
| - 두 연결 리스트의 노드를 비교하면서 순차적으로 탐색한다 | ||
| - 최악의 경우 두 리스트의 전체 노드 개수에 비례하여 연산을 수행한다 | ||
| SC: O(1) | ||
| - 기존 노드를 재사용하여 연결하므로 별도의 리스트를 생성하지 않는다 | ||
| - dummy 노드와 current 포인터 등 상수 크기의 추가 공간만 사용한다 | ||
|
|
||
| """ | ||
| class Solution: | ||
| def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) -> ListNode | None: | ||
| dummy = ListNode() | ||
| current = dummy | ||
|
|
||
| while list1 and list2: | ||
| if list1.val < list2.val: | ||
| current.next = list1 | ||
| list1.next = list1 | ||
| else: | ||
| current.next = list2 | ||
| list2.next = list2 | ||
|
|
||
| current = current.next | ||
|
|
||
| current.next = list1 if list1 else list2 | ||
|
|
||
| return dummy.next |
This file contains hidden or 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,47 @@ | ||
| """ | ||
| M: 행의 개수 | ||
| N: 열의 개수 | ||
| L: 단어의 길이 | ||
| TC: O(M * N * 3^L) | ||
| SC: O(L) | ||
| """ | ||
| class Solution: | ||
| def exist(self, board: list[list[str]], word: str) -> bool: | ||
| M = len(board) | ||
| N = len(board[0]) | ||
| L = len(word) | ||
|
|
||
| def dfs(x, y, index): | ||
| if index == L: | ||
| return True | ||
|
|
||
| if not (0 <= x < M and 0 <= y < N): | ||
| return False | ||
| if board[x][y] != word[index]: | ||
| return False | ||
|
|
||
| # 기존 문자 저장하고 방문 처리 | ||
| temp = board[x][y] | ||
| board[x][y] = '#' | ||
|
|
||
| found = False | ||
| # found = (dfs(x + 1, y, index + 1) or dfs(x - 1, y, index + 1) | ||
| # or dfs(x, y + 1, index + 1) or dfs(x, y - 1, index + 1)) | ||
| for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: | ||
| nx, ny = x + dx, y + dy | ||
| if dfs(nx, ny, index + 1): | ||
| found = True | ||
| break | ||
|
|
||
| # 기존 문자 복구 | ||
| board[x][y] = temp | ||
|
|
||
| return found | ||
|
|
||
| for i in range(M): | ||
| for j in range(N): | ||
| if dfs(i, j, 0): | ||
| return True | ||
|
|
||
| return False | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 stack으로 풀어서 경로를 다 본 뒤 복구하려고 복구 항목을 따로 넣었는데, 재귀로 푸니까 호출이 끝나고 돌아온 시점에 바로 복구할 수 있어서 더 직관적이네요!