-
-
Notifications
You must be signed in to change notification settings - Fork 362
[parkhojeong] WEEK 06 Solutions #2792
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
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,20 @@ | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| i = 0 | ||
| j = len(height) - 1 | ||
| max_amount = 0 | ||
|
|
||
| while i < j: | ||
| left_height = height[i] | ||
| right_height = height[j] | ||
|
|
||
| amount = min(left_height, right_height) * (j - i) | ||
| if amount > max_amount: | ||
| max_amount = amount | ||
|
|
||
| if left_height > right_height: | ||
| j -= 1 | ||
| else: | ||
| i += 1 | ||
|
|
||
| return max_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.
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의 예시입니다. def _query(
self,
node_index: int,
seg_left: int,
seg_right: int,
qur_left: int,
qur_right: int,
) -> int:
if seg_right < qur_left or qur_right < seg_left:
return 0
if qur_left <= seg_left <= seg_right <= qur_right:
return self.tree[node_index]
mid = (seg_left + seg_right) // 2
left = self._query(node_index * 2, seg_left, mid, qur_left, qur_right)
right = self._query(node_index * 2 + 1, mid + 1, seg_right, qur_left, qur_right)
return max(left, right)
def query(self, left: int, right: int) -> int:
if left > right:
return 0
return self._query(1, 0, self.n - 1, left, right) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| class Node: | ||
| ch = "" | ||
| is_end = False | ||
| child: List[Node] = None | ||
| child: dict[str, Node] = None | ||
|
Comment on lines
+2
to
+5
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. 해당 부분은 클래스 변수를 선언하신것 같은데 삭제해도 무방할것 같아요! |
||
|
|
||
| def __init__(self, ch, is_end): | ||
| self.ch = ch | ||
| self.is_end = is_end | ||
| self.child = {} | ||
|
|
||
| def getIsEnd(self): | ||
| return self.is_end | ||
|
|
||
| def setIsEndTrue(self): | ||
| self.is_end = True | ||
|
|
||
| def getChild(self, ch) -> None: | ||
| if ch in self.child: | ||
| return self.child[ch] | ||
|
|
||
| return None | ||
|
|
||
| def getAllChild(self) -> List[Node]: | ||
| return self.child.values() | ||
|
|
||
| def addChild(self, node): | ||
| self.child[node.ch] = node | ||
|
|
||
| class Trie: | ||
| def __init__(self): | ||
| self.root = Node("", False) | ||
|
|
||
| def addWord(self, word) -> None: | ||
| i = 0 | ||
| node = self.root | ||
| while i < len(word): | ||
| ch = word[i] | ||
| child_node = node.getChild(ch) | ||
| if child_node: | ||
| node = child_node | ||
| if i == len(word) - 1: | ||
| node.setIsEndTrue() | ||
| else: | ||
| is_end = True if i == len(word) - 1 else False | ||
| child_node = Node(ch, is_end) | ||
| node.addChild(child_node) | ||
| node = child_node | ||
|
|
||
| i += 1 | ||
|
|
||
| def search(self, word: str, i, node) -> bool: | ||
| if word[i] == ".": | ||
| child_nodes = node.getAllChild() | ||
| else: | ||
| child_node = node.getChild(word[i]) | ||
| if child_node is None: | ||
| return False | ||
| child_nodes = [child_node] | ||
|
|
||
| for child_node in child_nodes: | ||
| if i == len(word) - 1: | ||
| if child_node.getIsEnd() is True: | ||
| return True | ||
| else: | ||
| continue | ||
|
|
||
| if self.search(word, i + 1, child_node): | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| class WordDictionary: | ||
| def __init__(self): | ||
| self.trie = Trie() | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| self.trie.addWord(word) | ||
|
|
||
| def search(self, word: str) -> bool: | ||
|
|
||
| return self.trie.search(word, 0, self.trie.root) | ||
|
|
||
|
|
||
| # 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 i에 대해 앞선 j들을 확인하며 LIS 길이를 갱신하는 전형적인 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. N Log N 풀이도 꼭 한번 시도 해 보시죠! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| dp = [0] * len(nums) | ||
| max_count = 0 | ||
|
|
||
| for i in range(len(nums)): | ||
| dp[i] = 1 | ||
| for j in range(i): | ||
| if nums[j] < nums[i]: | ||
| dp[i] = max(dp[j] + 1, dp[i]) | ||
|
|
||
| max_count = max(max_count, dp[i]) | ||
|
|
||
| return max_count |
|
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,49 @@ | ||
| VISITED = '#' | ||
|
|
||
| class Solution: | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| row = 0 | ||
| col = 0 | ||
|
|
||
| visit = [] | ||
|
|
||
| def canGo(row, col): | ||
| if row < 0 or row >= len(matrix) or col < 0 or col >= len(matrix[0]): | ||
| return False | ||
|
|
||
| if matrix[row][col] == VISITED: | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| while True: | ||
| if not (canGo(row, col + 1) or canGo(row + 1, col) or canGo(row, col - 1) or canGo(row - 1, col)): | ||
| break | ||
|
|
||
| while canGo(row, col + 1): | ||
| visit.append(matrix[row][col]) | ||
|
|
||
| matrix[row][col] = VISITED | ||
| col += 1 | ||
|
|
||
| while canGo(row + 1, col): | ||
| visit.append(matrix[row][col]) | ||
|
|
||
| matrix[row][col] = VISITED | ||
| row += 1 | ||
|
|
||
| while canGo(row, col - 1): | ||
| visit.append(matrix[row][col]) | ||
|
|
||
| matrix[row][col] = VISITED | ||
| col -= 1 | ||
|
|
||
| while canGo(row - 1, col): | ||
| visit.append(matrix[row][col]) | ||
|
|
||
| matrix[row][col] = VISITED | ||
| row -= 1 | ||
|
|
||
| visit.append(matrix[row][col]) | ||
|
|
||
| return visit |
|
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,26 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
|
|
||
| open_parenthesis_set = set({"{", "[", "("}) | ||
| close_parenthesis_map = { | ||
| "]": "[", | ||
| ")": "(", | ||
| "}": "{" | ||
| } | ||
|
|
||
| for ch in s: | ||
| if ch in open_parenthesis_set: | ||
| stack.append(ch) | ||
| else: | ||
| if len(stack) == 0: | ||
| return False | ||
|
|
||
| top = stack.pop() | ||
| if close_parenthesis_map[ch] != top: | ||
| return False | ||
|
|
||
| if len(stack) > 0: | ||
| return False | ||
|
|
||
| return True |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.maxArea— Time: O(n) / Space: O(1)피드백: 왼쪽/오른쪽 포인터를 한 칸씩 이동시키며 최대 넓이를 갱신합니다. 추가 공간 없이 상수 공간으로 구현되어 있습니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
WordDictionary.addWord— Time: O(n) / Space: O(sum 길이)피드백: Trie를 사용해 단어를 저장하고 '.'를 모든 자식으로 확장하며 검색합니다. 구현에 여러 보조 메서드가 섞여 있어 유지보수 측면에서 다소 복잡합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
WordDictionary.search— Time: O(m) / Space: O(sum 길이)피드백: 재귀로 '.'를 모든 자식 노드에 대해 탐색합니다. 최악의 경우 지수적으로 증가할 수 있습니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 4:
Trie.addWord— Time: O(n) / Space: O(sum 길이)피드백: 연속된 문자로 트리를 구성하고 마지막 문자에 종료 플래그를 설정합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 5:
Trie.__init__— Time: O(1) / Space: O(1)피드백: 루트 노드를 하나 생성합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 6:
Node.__init__— Time: O(1) / Space: O(1)피드백: 노드의 문자 정보와 종료 여부, 자식 맵을 초기화합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 7:
Node.getChild— Time: O(1) / Space: O(1)피드백: 해당 문자 존재 여부를 해시맵에서 즉시 조회합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 8:
Node.getAllChild— Time: O(1) / Space: O(k)피드백: 자식 컬렉션의 뷰를 반환합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 9:
Node.addChild— Time: O(1) / Space: O(1)피드백: 자식 맵에 노드를 삽입합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 10:
WordDictionary— Time: O(1) / Space: O(sum 길이)피드백: Trie를 래핑해 addWord와 search를 제공합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 11:
WordDictionary.__init__— Time: O(1) / Space: O(1)피드백: 트라이를 내부에 구성합니다.
개선 제안: 현재 구현이 적절해 보입니다.