Repository navigation
[yuseok89] WEEK 14 Solutions #2867
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
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. 엄청 깔끔하게 잘 해결하셨네요!
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. 진짜 회원님 코드 볼때마다 느끼는거지만 Leetcode Study 처음 오셨을때랑 지금이랑 차이가 어마어마하신것 같아서 대단한거 같아요 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # TC: O(N) | ||
| # SC: O(N) | ||
| # 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 levelOrder(self, root: TreeNode | None) -> list[list[int]]: | ||
|
|
||
| ret = [] | ||
|
|
||
| def preOrder(root: TreeNode | None, height: int): | ||
| if len(ret) == height: | ||
| ret.append([]) | ||
|
|
||
| ret[height].append(root.val) | ||
|
|
||
| if root.left: | ||
| preOrder(root.left, height + 1) | ||
| if root.right: | ||
| preOrder(root.right, height + 1) | ||
|
|
||
| if root: | ||
| preOrder(root, 0) | ||
|
|
||
| return ret | ||
|
|
|
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. 🏷️ 알고리즘 패턴 분석counting-bits/yuseok89.py# TC: O(N)
# SC: O(N)
class Solution:
def countBits(self, n: int) -> list[int]:
ans = []
upper = 1
ans.append(0)
for idx in range(1, n + 1):
ans.append(ans[idx - upper] + 1)
if idx == upper:
upper *= 2
return ans
📊 시간/공간 복잡도 분석
피드백: 동적 계획으로 각 수의 비트를 빠르게 계산한다. 상향식으로 이전 값을 활용해 O(n) 시간에 n개의 결과를 얻는다. 개선 제안: 현재 구현이 적절해 보입니다.
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,17 @@ | ||
| # TC: O(N) | ||
| # SC: O(N) | ||
| class Solution: | ||
| def countBits(self, n: int) -> list[int]: | ||
| ans = [] | ||
|
|
||
| upper = 1 | ||
| ans.append(0) | ||
|
|
||
| for idx in range(1, n + 1): | ||
| ans.append(ans[idx - upper] + 1) | ||
|
|
||
| if idx == upper: | ||
| upper *= 2 | ||
|
|
||
| return ans | ||
|
|
|
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. 🏷️ 알고리즘 패턴 분석house-robber-ii/yuseok89.py# TC: O(N)
# SC: O(1)
class Solution:
def rob(self, nums: list[int]) -> int:
n = len(nums)
def calc(nums, s, e):
rob, skip = 0, 0
for idx in range(s, e):
rob, skip = skip + nums[idx], max(rob, skip)
return max(rob, skip)
if n == 1:
return nums[0]
return max(calc(nums, 0, n - 1), calc(nums, 1, n))
📊 시간/공간 복잡도 분석
피드백: 두 구간으로 나누어 DP를 수행하고, 원소의 개수에 따라 첫 원소 포함/미포함 두 경우를 비교한다. 공간은 상수로 유지된다. 개선 제안: 현재 구현이 적절해 보입니다.
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,19 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| class Solution: | ||
| def rob(self, nums: list[int]) -> int: | ||
|
|
||
| n = len(nums) | ||
|
|
||
| def calc(nums, s, e): | ||
| rob, skip = 0, 0 | ||
| for idx in range(s, e): | ||
| rob, skip = skip + nums[idx], max(rob, skip) | ||
|
|
||
| return max(rob, skip) | ||
|
|
||
| if n == 1: | ||
| return nums[0] | ||
|
|
||
| return max(calc(nums, 0, n - 1), calc(nums, 1, n)) | ||
|
|
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.
🏷️ 알고리즘 패턴 분석
binary-tree-level-order-traversal/yuseok89.py
📊 시간/공간 복잡도 분석
피드백: 재귀를 통해 트리를 순회하고 각 높이에 따라 리스트를 확장하며 값을 추가한다. 레벨 순서를 얻으려면 큐를 이용한 BFS가 일반적이지만 현재 구현은 프리오더 순회로 동작한다.
개선 제안: 현재 구현이 적절해 보입니다.