-
-
Notifications
You must be signed in to change notification settings - Fork 362
[parkhojeong] WEEK 04 Solutions #2749
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
Changes from all commits
127714c
3b7372a
c3e1000
c4b824e
87afcad
c98db17
31ba2f4
90628c0
994aca2
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 초기 dp 배열을 1/0으로 구성하는 접근은 각 금액에 도달 가능한지 여부를 저장하는 방식으로 작동한다. 다만 초기화 및 업데이트 로직이 잘못될 경우 일부 경우에서 -1 처리와 경계 조건이 누락될 수 있다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution: | ||
| def coinChange(self, coins: List[int], amount: int) -> int: | ||
| if amount == 0: | ||
| return 0 | ||
|
|
||
| dp = [1 if x in coins else 0 for x in range(amount + 1)] | ||
| MAX_VALUE = sys.maxsize | ||
|
|
||
| for i in range(1, amount + 1): | ||
| if dp[i] != 0: | ||
| continue | ||
|
|
||
| dp[i] = MAX_VALUE | ||
| for coin in coins: | ||
| if i - coin > 0 and dp[i - coin] > 0: | ||
| dp[i] = min(dp[i - coin] + 1, dp[i]) | ||
|
|
||
| if dp[amount] == MAX_VALUE: | ||
| return -1 | ||
|
|
||
| return dp[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. 혹시 슬라이싱을 쓰신 이유가 있으실까요?
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.
@alphaorderly 이미 머지된 PR이지만 공부하려고 구경 중에 질문드립니다. 감사합니다😆
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. 이건 사담인데
else:
nums = nums[mid_idx + 1:]
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. 파이썬이 range 넘어가는 경우엔 빈 배열로 되는 것을 사용해보았습니다. 근데 이 풀이는 슬라이싱이 O(n)의 복잡도라 최적화 진행해봤습니다. 31ba2f4
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 적절한 이분탐색 로직으로 회전 포인트를 찾고 있다. 루프 내에서 최소값 비교 조건이 명확하다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| lo, hi = 0, len(nums) - 1 | ||
| while lo < hi: | ||
| mid = (lo + hi) // 2 | ||
|
|
||
| if nums[lo] < nums[hi]: | ||
| return nums[lo] | ||
|
|
||
| if nums[mid] < nums[hi]: | ||
| hi = mid | ||
| else: | ||
| lo = mid + 1 | ||
|
|
||
| return nums[lo] |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리의 모든 노드를 방문하며 각 재귀 호출이 깊이를 증가시키는 전형적인 DFS 방식이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 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: | ||
| return self.dfs(root, 0) | ||
|
|
||
| def dfs(self, root, depth) -> int: | ||
| if not root: | ||
| return depth | ||
|
|
||
| return max(self.dfs(root.left, depth + 1), self.dfs(root.right, depth + 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. 너무 잘 풀어 주셨는데, 다만 조금 아쉬운점은 로직이 너무 복잡해서 코드 가독성이 떨어지는게 아닐까 싶어요
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,25 @@ | ||
| # 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]: | ||
| dummy_head = ListNode() | ||
| cur = dummy_head | ||
|
|
||
| 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 | ||
|
|
||
| if list1: | ||
| cur.next = list1 | ||
| elif list2: | ||
| cur.next = list2 | ||
|
|
||
| return dummy_head.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. 이거 풀이 1900ms 나오는데 0ms까지 줄일수 있는거 아시나요? 한번 시도해보시는것도 재밌으실거에요!
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
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. 대문자 케이싱을 사용하면 가독성이 괜찮더라구요. 빈도 기반으로 체크하는 로직은 초기 한 번만 하면 되서 정리해보았습니다. 994aca2
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,51 @@ | ||
| from collections import Counter | ||
| from typing import List | ||
|
|
||
| class Solution: | ||
| def exist(self, board: List[List[str]], word: str) -> bool: | ||
| row_len, col_len = len(board), len(board[0]) | ||
|
|
||
| board_freq = Counter(ch for row in board for ch in row) | ||
|
|
||
| for ch, need in Counter(ch for ch in word).items(): | ||
| if board_freq.get(ch, 0) < need: | ||
| return False | ||
|
|
||
| if board_freq.get(word[0], 0) > board_freq.get(word[-1], 0): | ||
| word = word[::-1] | ||
|
|
||
| DIRECTIONS = ((-1, 0), (1, 0), (0, -1), (0, 1)) | ||
| MARKER = "" | ||
|
|
||
| def in_bounds(r: int, c: int) -> bool: | ||
| return 0 <= r < row_len and 0 <= c < col_len | ||
|
|
||
| def backtrack(r: int, c: int, idx: int) -> bool: | ||
| if idx == len(word) - 1: | ||
| return True | ||
|
|
||
| saved_char = board[r][c] | ||
| board[r][c] = MARKER | ||
| board_freq[saved_char] -= 1 | ||
|
|
||
| for dr, dc in DIRECTIONS: | ||
| nr, nc = r + dr, c + dc | ||
| next_ch = word[idx + 1] | ||
|
|
||
| if (in_bounds(nr, nc) | ||
| and board[nr][nc] != MARKER | ||
| and board[nr][nc] == next_ch | ||
| and board_freq[next_ch] > 0 | ||
| and backtrack(nr, nc, idx + 1)): | ||
| return True | ||
|
|
||
| board[r][c] = saved_char | ||
| board_freq[saved_char] += 1 | ||
| return False | ||
|
|
||
| for r in range(row_len): | ||
| for c in range(col_len): | ||
| if board[r][c] == word[0] and backtrack(r, c, 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.
sys.maxsize로 최대값을 설정할 수 있군요!많이 배워갑니다.