-
-
Notifications
You must be signed in to change notification settings - Fork 362
[njngwn] WEEK 06 Solutions - #2786 #2787
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
81cc13a
ad16a9e
efb28a4
be440df
3b618b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution: | ||
| # Time Complexity: O(n), n: len(height) | ||
| # Space Complexity: O(1) | ||
| def maxArea(self, height: List[int]) -> int: | ||
| left, right = 0, len(height) - 1 | ||
| max_area = 0 | ||
|
|
||
| while left < right: | ||
| w = right - left | ||
| h = min(height[left], height[right]) | ||
| max_area = max(max_area, w * h) | ||
|
|
||
| if height[left] <= height[right]: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
| return max_area |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(L) |
| Space | O(TotalNodes) |
피드백: 트라이를 이용해 문자열 삽입 및 탐색을 구현했다. 삽입/공간 비용은 단어 길이에 비례한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: WordDictionary.search — Time: O(Branching^W) / Space: O(H)
| 복잡도 | |
|---|---|
| Time | O(Branching^W) |
| Space | O(H) |
피드백: 와일드카드 탐색으로 최악의 경우 다수의 경로를 탐색할 수 있다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| class TrieNode: | ||
| def __init__(self): | ||
| self.children = {} | ||
| self.isEnd = False | ||
|
|
||
|
|
||
| class WordDictionary: | ||
|
|
||
| def __init__(self): | ||
| self.root = TrieNode() | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| node = self.root | ||
| for ch in word: | ||
| if ch not in node.children: | ||
| node.children[ch] = TrieNode() | ||
| node = node.children[ch] | ||
|
|
||
| node.isEnd = True | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| def dfs(node: TrieNode, idx: int) -> bool: | ||
| if idx == len(word): | ||
| return node.isEnd | ||
|
|
||
| ch = word[idx] | ||
| if ch == '.': | ||
| for child in node.children.values(): | ||
| if dfs(child, idx + 1): | ||
| return True | ||
| return False | ||
| else: | ||
| if ch not in node.children: | ||
| return False | ||
| return dfs(node.children[ch], idx + 1) | ||
|
|
||
|
Comment on lines
+30
to
+36
Member
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.
|
||
| return dfs(self.root, 0) | ||
|
|
||
| # Your WordDictionary object will be instantiated and called as such: | ||
| # obj = WordDictionary() | ||
| # obj.addWord(word) | ||
| # param_2 = obj.search(word) | ||
|
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,23 @@ | ||
| class Solution: | ||
| # Time Complexity: O(n*log(n)), n: len(nums) | ||
| # Space Complexity: O(1) | ||
|
Comment on lines
+2
to
+3
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. seq가 최악의 경우 길이 n만큼 되서 SC는 O(n)이지 않을까요? ex. [1, 2, 3, 4, 5] |
||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| seq = [] | ||
|
|
||
| for num in nums: | ||
| if not seq or (seq[-1] < num): | ||
| seq.append(num) | ||
| continue | ||
|
|
||
| # insert with binary search | ||
|
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. 로직은 찾는 값보다 큰 값 중 가장 작은 데에 덮어 쓰는 동작으로 보이는데요. 주석을 insert, binary search 용어를 사용해서 적으신 이유가 있을까요? 지금 주석은 해당 값을 찾아서(탐색) 그 인덱스에 insert(python) 하는 동작으로 읽혀서요. |
||
| left, right = 0, len(seq) | ||
| while left < right: | ||
| mid = (left + right) // 2 | ||
| if seq[mid] < num: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid | ||
|
Comment on lines
+13
to
+19
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. 오 python bisect_left 랑 동일하게 구현하셨는데 원래 bisect_left 알고 계셨는지 궁금하네요. https://github.com/python/cpython/blob/7ce7f0bd8511410bf7c42cc5fc88dfafe07874b6/Lib/bisect.py#L74 |
||
|
|
||
| seq[left] = num | ||
|
|
||
| return len(seq) | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 경계와 방향 관리가 복잡하지만 전체 원소를 한 번씩 방문한다. 개선 제안: 현재 구현이 적절해 보입니다.
Member
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,30 @@ | ||
| class Solution: | ||
| # Time Complexity: O(n*m), n: len(matrix), m: len(matrix[0]) | ||
| # Space Complexity: O(1) | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| result = [] | ||
| direction = [(0, -1), (0, 1), (-1, 0), (1, 0)] # left, right, up, down | ||
| left, right, top, bottom = 0, len(matrix[0])-1, 0, len(matrix)-1 # borders | ||
| i, j, d = 0, 0, direction[1] | ||
|
|
||
| while left <= right and top <= bottom and 0 <= i < len(matrix) and 0 <= j < len(matrix[0]): | ||
|
Comment on lines
+9
to
+10
Member
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. 현재 방향 전환과 경계 갱신 로직이 정상적으로 동작한다면 |
||
| # change direction and borders | ||
| if j == right and d == direction[1]: | ||
| d = direction[3] # right -> down | ||
| top += 1 | ||
| elif i == bottom and d == direction[3]: | ||
| d = direction[0] # down -> left | ||
| right -= 1 | ||
| elif j == left and d == direction[0]: | ||
| d = direction[2] # left -> up | ||
| bottom -= 1 | ||
| elif i == top and d == direction[2]: | ||
| d = direction[1] # up -> right | ||
| left += 1 | ||
|
|
||
| # append val to the result | ||
| result.append(matrix[i][j]) | ||
| i += d[0] | ||
| j += d[1] | ||
|
|
||
| return result | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 짝이 맞지 않으면 즉시 False를 반환하도록 구현되어 있다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from collections import deque | ||
|
|
||
|
|
||
| class Solution: | ||
| # Time Complexity: O(n), n: len(s) | ||
| # Space Complexity: O(n), n: len(s) | ||
| def isValid(self, s: str) -> bool: | ||
| if len(s) % 2 != 0: | ||
| return False | ||
|
Comment on lines
+8
to
+9
Member
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. 길이가 홀수인 경우 미리 반환해 불필요한 연산을 줄인 점이 좋네요! |
||
|
|
||
| stack = deque() | ||
|
|
||
| for ch in s: | ||
| if ch == '(' or ch == '[' or ch == '{': | ||
| stack.append(ch) | ||
| elif len(stack) != 0: | ||
| top = stack.pop() | ||
| if (top == '(' and ch == ')') or (top == '[' and ch == ']') or (top == '{' and ch == '}'): | ||
| continue | ||
| else: | ||
| return False | ||
| else: | ||
| return False | ||
|
|
||
| return len(stack) == 0 | ||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 좌우 포인터를 한 칸씩 이동시키며 전체를 한 번만 훑는 방법으로 최댓값을 구한다.
개선 제안: 현재 구현이 적절해 보입니다.