-
-
Notifications
You must be signed in to change notification settings - Fork 361
[yuseok89] WEEK 04 Solutions #2739
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
base: main
Are you sure you want to change the base?
Changes from all commits
4836151
cdc00da
cc0a988
c894055
4e4e412
32cafb9
49ecb59
a6fcc33
38a1f67
8bd5a32
05685ed
cd23e34
120df1f
1d4ff54
23142a7
0cb14ca
9465142
8b4a01f
4b09054
da3aad9
5c4d5fb
03f3f1a
d8d5ea4
1f003e2
a022d10
b8248c4
51c1012
65431c0
e514ab2
194db1c
419dfc5
af586d8
e2f0e02
41107c5
37ecdd8
fc56573
350342d
0a99924
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 상한이 amount인 DP/BFS 방식으로 각 금액에 도달하는 최소 동전 수를 갱신합니다. 금액 범위가 크면 공간과 시간이 커집니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # TC: O(amount * len(coins)) | ||
| # SC: O(amount) | ||
| class Solution: | ||
| def coinChange(self, coins: List[int], amount: int) -> int: | ||
|
|
||
| from collections import deque | ||
|
|
||
| v = {0: 0} | ||
| q = deque([0]) | ||
|
|
||
| while q and amount not in v: | ||
|
|
||
| cur = q.popleft() | ||
|
|
||
| for coin in coins: | ||
|
|
||
| if cur + coin > amount or cur + coin in v: | ||
| continue | ||
|
|
||
| v[cur + coin] = v[cur] + 1 | ||
| q.append(cur + coin) | ||
|
|
||
| return -1 if amount not in v else v[amount] | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 while loop 안에 복잡한 연산 들어가는거 별로라 최대한 간단하게 했는데, 이렇게 하시면 얼리 리턴이 가능해서 좋네요!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중간값 비교를 통해 회전 여부를 판단하고 범위를 절반으로 줄여 구간을 축소합니다. 개선 제안: 특정 경계 케이스에서의 비교 로직을 더 명확히 다듬으면 안정성이 증가합니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # TC: O(logN) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| low = 0 | ||
| high = len(nums) - 1 | ||
|
|
||
| while low < high - 1: | ||
| mid = (low + high) // 2 | ||
|
|
||
| if nums[low] < nums[mid] < nums[high]: | ||
| return nums[low] | ||
|
|
||
| if nums[low] > nums[mid]: | ||
| high = mid | ||
| else: | ||
| low = mid | ||
|
|
||
| return min(nums[low], nums[high]) | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아주 개인적인 생각인데, rec을 쓰지 않고 maxDepth 자체를 재귀로 하면 좀 코드가 간단해지지 않을까요?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그러네요. 문제 풀 당시에는 바로 생각을 못했었네요.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리를 방문하며 좌우 서브트리 깊이를 비교해 깊이를 반환합니다. 개선 제안: 꼭 필요한 경우 꼬리 재귀 최적화나 반복 방식으로도 구현 가능하지만 현 구현도 적절합니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # TC: O(N) | ||
| # SC: O(H) | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
|
|
||
| if not root: | ||
| return 0 | ||
|
|
||
| return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 2개 비교하는 부분 끝나고 리스트 두개중에 한개 순회 다 끝난 상태에서는 나머지 남은 리스트는 그냥 뒤에 통째로 붙혀주는게 더 좋더라구요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 의견 감사합니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 인접 포인터를 사용해 단일 연결 리스트로 합칩니다. 추가적인 데이터 구조 없이 연결 상태를 유지합니다. 개선 제안: 루프 종료 조건과 초기화 부분을 간결하게 다듬으면 가독성이 좋아집니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # TC: O(N+M) | ||
| # SC: O(1) | ||
| # 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]: | ||
|
|
||
| ret = ListNode() | ||
| cur = ret | ||
|
|
||
| while list1 and list2: | ||
| if list1.val < list2.val: | ||
| cur.next = list1 | ||
| list1 = list1.next | ||
| else: | ||
| cur.next = list2 | ||
| list2 = list2.next | ||
| cur = cur.next | ||
|
|
||
| cur.next = list1 if list1 else list2 | ||
|
|
||
| return ret.next | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 회원님 답안은 3000ms 가량 걸리는데
이 두가지만 추가해도 leetcode 실행속도상 0ms까지 도달할수 있어요!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 백트래킹에서 탐색공간을 프루닝하는건 좋은 공부가 될것 같습니다!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리뷰 감사합니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다! 빈도 기반으로 뒤집었는데, 해당 문제를 어떻게 하면 조금이라도 빨리 풀수 있을까에만 초점을 맞춘 코드이긴 합니다!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 단어의 글자 수 L에 따라 탐색 트리의 폭이 커질 수 있어 최악 시간은 증가합니다. 사전 카운트로 빠른 불가능 판단을 포함합니다. 개선 제안: 입력 크기에 따라 가지치기를 더 강화하거나 비트마스킹/방문 배열 재사용을 통해 상수 공간을 늘리지 않도록 개선 가능. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # TC: O(N*M*4^L) | ||
| # SC: O(L) | ||
| class Solution: | ||
| def exist(self, board: List[List[str]], word: str) -> bool: | ||
|
|
||
| n = len(board) | ||
| m = len(board[0]) | ||
|
|
||
| board_counter = Counter(board[r][c] for r in range(n) for c in range(m)) | ||
| word_counter = Counter(word) | ||
|
|
||
| for c, cnt in word_counter.items(): | ||
| if board_counter[c] < cnt: | ||
| return False | ||
|
|
||
| def rec(row, col, idx): | ||
| if idx == len(word): | ||
| return True | ||
|
|
||
| if row < 0 or row >= n or col < 0 or col >= m or board[row][col] != word[idx]: | ||
| return False | ||
|
|
||
| board[row][col] = None | ||
|
|
||
| ret = rec(row + 1, col, idx + 1) or rec(row - 1, col, idx + 1) or rec(row, col + 1, idx + 1) or rec(row, col - 1, idx + 1) | ||
|
|
||
| board[row][col] = word[idx] | ||
|
|
||
| return ret | ||
|
|
||
| for row in range(0, n): | ||
| for col in range(0, m): | ||
| if board[row][col] == word[0] and rec(row, col, 0): | ||
| return True | ||
|
|
||
| return False | ||
|
|

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.
저는 코인이 양수값만 있어서 DP만 했었는데 BFS로 해도 충분히 가능하겠네요!