-
-
Notifications
You must be signed in to change notification settings - Fork 362
[daehyun99] WEEK 06 Solutions #2789
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Time: O(N) | ||
| # Space: O(1) | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| size = 0 | ||
| l, r = 0, len(height)-1 | ||
| length = len(height)-1 | ||
|
|
||
| while l < r: | ||
| if height[l] < height[r]: | ||
| size = max(size, height[l] * length) | ||
| l += 1 | ||
| length -= 1 | ||
| else: | ||
| size = max(size, height[r] * length) | ||
| r -= 1 | ||
| length -= 1 | ||
| return size |
|
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(k) |
| Space | O(number of nodes) |
피드백: 키 문자마다 트리에 노드를 생성하거나 이동하며 공간 복잡도는 단어 길이에 비례한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: WordDictionary.search — Time: 복잡도는 문자열과 트리 구조에 따라 달라짐 / Space: O(depth of tree)
| 복잡도 | |
|---|---|
| Time | 복잡도는 문자열과 트리 구조에 따라 달라짐 |
| Space | O(depth of tree) |
피드백: '.'의 확장으로 가능 경로가 급증할 수 있어 최악의 경우 비효율적일 수 있다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class WordDictionary: | ||
|
|
||
| def __init__(self): | ||
| self.root = {} | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| pointer = self.root | ||
| for char in word: | ||
| if char in pointer: | ||
| pointer = pointer[char] | ||
| else: | ||
| pointer[char] = {} | ||
| pointer = pointer[char] | ||
| pointer["0"]={} | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| from collections import deque | ||
| pointer = self.root | ||
| que = deque() | ||
| que.append(pointer) | ||
|
|
||
| for char in word: | ||
| for i in range(len(que)): | ||
| pointer = que.popleft() | ||
| if char in pointer: | ||
| que.append(pointer[char]) | ||
| elif char == ".": | ||
| for key in pointer.keys(): | ||
| que.append(pointer[key]) | ||
| while len(que) > 0: | ||
| pointer = que.popleft() | ||
| if "0" in pointer: | ||
| return True | ||
| return False | ||
|
|
||
| # 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 < i를 확인해 증가하는 경우 최댓값으로 갱신한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Time: O(N**2) | ||
| # Space: O(N) | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| counts = [1 for i in nums] | ||
|
|
||
| for i in range(1, len(nums)): | ||
| for j in range(0, i): | ||
| if nums[j] < nums[i]: | ||
| counts[i] = max(counts[i], counts[j]+1) | ||
| return max(counts) |
|
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,55 @@ | ||
| class Solution: | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| height = len(matrix) | ||
| weight = len(matrix[0]) | ||
| adding = [ | ||
| [0, 1], | ||
| [1, 0], | ||
| [0, -1], | ||
| [-1, 0] | ||
| ] | ||
| m = 0 | ||
| n = -1 | ||
| size = height * weight | ||
| count = 0 | ||
|
|
||
| result = [] | ||
|
|
||
| while count < size: | ||
| if 0 < weight and count < size: | ||
| m_add, n_add = adding[0] | ||
| for _ in range(weight): | ||
| m += m_add | ||
| n += n_add | ||
| result.append(matrix[m][n]) | ||
| count += 1 | ||
|
|
||
| height -= 1 | ||
| if 0 < height and count < size: | ||
| m_add, n_add = adding[1] | ||
| for _ in range(height): | ||
| m += m_add | ||
| n += n_add | ||
| result.append(matrix[m][n]) | ||
| count += 1 | ||
|
|
||
| weight -= 1 | ||
| if 0 < weight and count < size: | ||
| m_add, n_add = adding[2] | ||
| for _ in range(weight): | ||
| m += m_add | ||
| n += n_add | ||
| result.append(matrix[m][n]) | ||
| count += 1 | ||
|
|
||
| height -= 1 | ||
| if 0 < height and count < size: | ||
| m_add, n_add = adding[3] | ||
| for _ in range(height): | ||
| m += m_add | ||
| n += n_add | ||
| result.append(matrix[m][n]) | ||
| count += 1 | ||
|
|
||
| weight -= 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 괄호 매칭을 스택으로 처리하는 일반적인 방법이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
|
|
||
| map = {"()", "{}", "[]"} | ||
|
|
||
| for char in s: | ||
| stack.append(char) | ||
| if len(stack) >= 2: | ||
| if ''.join(stack[-2:]) in map: | ||
| stack.pop() | ||
| stack.pop() | ||
| if len(stack) == 0: | ||
| return True | ||
| else: | ||
| return False |
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) → O(n) / Space: ✅ O(1) → O(1)피드백: 투 포인터로 양 끝을 유지하며 면적을 계산하고 포인터를 움직여 중복 계산을 피한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
WordDictionary.addWord— Time: O(k) / Space: O(sum of nodes)피드백: 해당 단어의 각 문자마다 트리에 노드를 생성하거나 이동한다. 단어 길이를 k라 할 때 시간은 O(k).
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
WordDictionary.search— Time: O(?) / Space: O(?)피드백: 검색 시 불확실한 경우 모든 경로를 탐색하므로 최악의 경우 지수적일 수 있다. 문자열 길이와 트리의 구조에 따라 다름.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 4:
Solution.lengthOfLIS— Time: ❌ O(N) → O(n^2) / Space: ❌ O(1) → O(n)피드백: 두 중첩 루프를 통해 모든 이전 원소를 확인하며 LIS를 갱신한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 5:
Solution.spiralOrder— Time: O(m*n) / Space: O(1)피드백: 네 방향으로의 경계 축소를 통해 모든 원소를 방문한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 6:
Solution.isValid— Time: O(n) / Space: O(n)피드백: 스택에 남은 괄호 쌍으로 유효성 판단을 수행한다.
개선 제안: 현재 구현이 적절해 보입니다.