-
-
Notifications
You must be signed in to change notification settings - Fork 362
[Chanz] WEEK 06 Solutions #2791
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,21 @@ | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| # 두 점의 거리가 길어야 하고, 높은 막대기들끼리 있어야 많은 물을 채울 수 있음. | ||
| # 양쪽의 점부터 시작해서 더 줄여볼지 말지를 판단 해 볼 수 있을 것 같음. | ||
|
|
||
| def caculate_water_container(left, right): | ||
| return (right - left) * min(height[left], height[right]) | ||
|
|
||
| left = 0 | ||
| right = len(height) - 1 | ||
| max_water = 0 | ||
|
|
||
| while left <= right: | ||
|
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. left == right인 경우는 필요 없어서 |
||
| max_water = max(max_water, caculate_water_container(left, right)) | ||
| if height[left] > height[right] : | ||
| right -= 1 | ||
| else : | ||
| left += 1 | ||
|
|
||
| return max_water | ||
|
|
||
|
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,40 @@ | ||
| class WordDictionary: | ||
|
|
||
| def __init__(self): | ||
| self.word_tree = {} | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| curr_branch = self.word_tree | ||
| for ch in word: | ||
| if not curr_branch.get(ch, None): | ||
| curr_branch[ch] = {} | ||
| curr_branch = curr_branch[ch] | ||
| curr_branch["#"] = {} # end of word | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| curr_branch = self.word_tree | ||
|
|
||
| def nested_search(start_branch: dict, word: str) -> bool: | ||
|
|
||
| for idx, ch in enumerate(word): | ||
| if ch in start_branch: | ||
| start_branch = start_branch[ch] | ||
| elif ch == ".": | ||
| for branch in start_branch.values(): | ||
| if nested_search(branch, word[idx+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. slicing을 쓰면 O(n)이라 slicing을 안 쓰도록 풀어보셔도 좋을 거 같아요. |
||
| return True | ||
| return False | ||
| else: | ||
| return False | ||
|
|
||
| if "#" in start_branch: | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| return nested_search(curr_branch, word) | ||
|
|
||
| # 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,20 @@ | ||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||
| def isValid(self, s: str) -> bool: | ||||||||||||||||||||||
| ch_stack = [] | ||||||||||||||||||||||
| ch_map = {} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ch_map[")"] = "(" | ||||||||||||||||||||||
| ch_map["}"] = "{" | ||||||||||||||||||||||
| ch_map["]"] = "[" | ||||||||||||||||||||||
|
Comment on lines
+4
to
+8
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. 선언 시점에 할당하면 좀 더 간결해 질 거 같네요.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for ch in s: | ||||||||||||||||||||||
| if ch == ")" or ch == "}" or ch == "]": | ||||||||||||||||||||||
| if not ch_stack: | ||||||||||||||||||||||
| return False | ||||||||||||||||||||||
| if ch_stack.pop() != ch_map[ch]: | ||||||||||||||||||||||
| return False | ||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| ch_stack.append(ch) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return not ch_stack | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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(len(word)) / Space: O(total_chars_stored)피드백: 각 글자마다 노드를 생성하고 끝에 '#'를 두어 단어 종료를 표시합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
WordDictionary.search— Time: O(branching_factor^length) / Space: O(length_of_word_depth)피드백: 점 와일드카드를 처리하기 위해 분기 탐색을 재귀적으로 수행합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 4:
Solution.isValid— Time: O(n) / Space: O(n)피드백: 닫는 괄호를 만났을 때 스택이 비어있거나 매칭되지 않으면 실패하고, 끝난 후 스택이 비어있어야 유효합니다.
개선 제안: 현재 구현이 적절해 보입니다.