-
-
Notifications
You must be signed in to change notification settings - Fork 362
[alphaorderly] WEEK 06 Solutions #2777
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
4d5f428
3ac63f5
2536209
612dbbb
a7c2098
79f6f86
3855235
33ab6bc
b1de0e1
f9bf16c
de3390f
b3dc7d7
17669b6
d8b5903
a14205b
b087a32
2380e81
e51ae37
9592367
555e26d
9b11b3c
aeffd8e
9fb334a
cad3e9b
5d805dd
d64aecd
f9db228
266bdae
bff71b4
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,33 @@ | ||
| """ | ||
| Time Complexity: O(n) | ||
| - n = number of elements in height | ||
| - Each index is visited at most once by either the left or right pointer. | ||
|
|
||
| Space Complexity: O(1) | ||
| - Only a small constant number of variables are used (left, right, ans). | ||
|
|
||
| Approach: | ||
| - Initialize two pointers at the beginning (left) and end (right) of the array. | ||
| - At each step, compute the area formed by the lines at left and right. | ||
| - Update the maximum area found so far. | ||
| - Move the pointer pointing to the shorter line inward (increase left or decrease right). | ||
| - Ensure to move shorter line inward to potentially find a taller line that could increase the area. | ||
| - Because amount of water is determined by the shorter line. | ||
| - Continue until the two pointers meet. | ||
| - Return the maximum area found. | ||
| """ | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| N = len(height) | ||
| left, right = 0, N - 1 | ||
| ans = 0 | ||
|
|
||
| while left < right: | ||
| ans = max(ans, min(height[left], height[right]) * (right - left)) | ||
|
|
||
| if height[left] < height[right]: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 글자 단위로 트라이에 노드를 생성하고 최종 노드에 end 플래그를 설정합니다. BFS로 '.' 처리 시 모든 자식 노드를 확장합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """ | ||
| Trie-based add and search with wildcard support. | ||
|
|
||
| ### BFS approach ### | ||
|
|
||
| Time Complexity: | ||
| - addWord: O(L), where L is the length of the inserted word. | ||
| - search: O(N), where N is the length of the search word; search with '.' wildcard may lead to O(B^N) in the worst case, | ||
| where B is average branching factor. For ordinary searches (no wildcards), still O(N). | ||
|
|
||
| Space Complexity: O(T) | ||
| - T = total number of characters in all inserted words, due to nodes in the trie. | ||
| """ | ||
|
|
||
| class WordDictionary: | ||
|
|
||
| def __init__(self): | ||
| self.children = dict() | ||
| self.end = False | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| node = self | ||
| for ch in word: | ||
| if ch not in node.children: | ||
| node.children[ch] = WordDictionary() | ||
| node = node.children[ch] | ||
| node.end = True | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| search = deque([self]) | ||
| for ch in word: | ||
| S = len(search) | ||
| for _ in range(S): | ||
| node = search.popleft() | ||
| if ch == '.': | ||
| for child in node.children.values(): | ||
| search.append(child) | ||
| elif ch not in node.children: | ||
| continue | ||
| else: | ||
| search.append(node.children[ch]) | ||
| return any(node.end for node in search) | ||
|
|
||
| """ | ||
| Trie-based add and recursive search with wildcard support. | ||
|
|
||
| ### DFS approach ### | ||
|
|
||
| Time Complexity: | ||
| - addWord: O(L), where L is the length of the inserted word. | ||
| - search: Worst case O(B^N), where N is the length of the search word and B is average branching factor if all | ||
| positions are wildcards. Otherwise O(N) for ordinary search. | ||
|
|
||
| Space Complexity: O(T) | ||
| - T = total number of characters in all inserted words (trie size), plus call stack space O(N) during search. | ||
| """ | ||
|
|
||
| class WordDictionary: | ||
|
|
||
| def __init__(self): | ||
| self.children = dict() | ||
| self.end = False | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| node = self | ||
| for ch in word: | ||
| if ch not in node.children: | ||
| node.children[ch] = WordDictionary() | ||
| node = node.children[ch] | ||
| node.end = True | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| def dfs(node: WordDictionary, index: int) -> bool: | ||
| if index == len(word): | ||
| return node.end | ||
| ch = word[index] | ||
| if ch == ".": | ||
| for child in node.children.values(): | ||
| if dfs(child, index + 1): | ||
| return True | ||
| elif ch in node.children: | ||
| return dfs(node.children[ch], index + 1) | ||
| return False | ||
|
|
||
| return dfs(self, 0) |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 수열의 최소 가능한 꼬리 값을 유지해 LIS 길이를 얻습니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """ | ||
| Time Complexity: O(n log n) | ||
| Space Complexity: O(n) | ||
|
|
||
| ### Binary search approach ### | ||
|
|
||
| For each number in nums: | ||
| - Find the insertion position for num in arr to maintain sorted order (binary search). | ||
| - If num is greater than all elements in arr, append it (extend the LIS). | ||
| - Otherwise, replace the element at position with num to keep tails as small as possible. | ||
| """ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| arr = [] | ||
|
|
||
| for num in nums: | ||
| position = bisect_left(arr, num) | ||
| if position == len(arr): | ||
| arr.append(num) | ||
| else: | ||
| arr[position] = num | ||
|
|
||
| return len(arr) |
|
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,38 @@ | ||
| """ | ||
| Spiral traversal of a matrix: visit all elements in "spiral order" (clockwise from top-left). | ||
|
|
||
| Time Complexity: O(N), where N is the total number of elements in matrix. | ||
| - Each element is visited once. | ||
|
|
||
| Space Complexity: O(N) | ||
| - The output array includes all N elements. | ||
| - The input matrix is modified in-place to mark visited cells. | ||
| """ | ||
| class Solution: | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| ROW = len(matrix) | ||
| COL = len(matrix[0]) | ||
| TARGET = ROW * COL | ||
| DIR = [[0, 1], [1, 0], [0, -1], [-1, 0]] | ||
| CHECK = -1000 | ||
|
|
||
| ans = [] | ||
|
|
||
| def bound(r: int, c: int) -> bool: | ||
| return 0 <= r < ROW and 0 <= c < COL | ||
|
|
||
| r = c = d = 0 | ||
|
|
||
| for _ in range(TARGET): | ||
| ans.append(matrix[r][c]) | ||
| matrix[r][c] = CHECK | ||
|
|
||
| tr = r + DIR[d][0] | ||
| tc = c + DIR[d][1] | ||
| if not bound(tr, tc) or matrix[tr][tc] == CHECK: | ||
| d = (d + 1) % 4 | ||
| r += DIR[d][0] | ||
| c += DIR[d][1] | ||
|
|
||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 여는 괄호를 스택에 push하고 닫는 괄호가 올 때 매칭 여부를 확인합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """ | ||
| Time Complexity: O(n) | ||
| - Each character in the input string s is visited exactly once in a single left-to-right scan. | ||
| - Each push or pop operation on the stack corresponds to a character, for at most O(n) stack operations. | ||
|
|
||
| Space Complexity: O(n) | ||
| - In the worst case (e.g., all opening brackets), the stack may contain up to n elements. | ||
| - The auxiliary data structure (`pairs`) uses constant space, as there are only three types of parentheses. | ||
|
|
||
| Approach: | ||
| The algorithm uses a stack to simulate balancing of brackets. | ||
| - For every opening bracket encountered, push it onto the stack. | ||
| - For every closing bracket, check if the top of the stack holds its matching opening bracket. | ||
| - If not, or if the stack is empty, the string is invalid. | ||
| - At the end, if the stack is empty, all open brackets were matched and closed properly. | ||
| """ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| pairs = {"(": ")", "{": "}", "[": "]"} | ||
| stack = [] | ||
|
|
||
| for ch in s: | ||
| if ch in pairs: | ||
| stack.append(ch) | ||
| elif not stack or pairs[stack[-1]] != ch: | ||
| return False | ||
| else: | ||
| stack.pop() | ||
|
|
||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 좌우 포인터를 활용해 짧은 쪽의 높이가 더 큰 방향으로 이동시키며 범위를 축소해 최댓값을 갱신합니다.
개선 제안: 현재 구현이 적절해 보입니다.