-
-
Notifications
You must be signed in to change notification settings - Fork 362
[ICE0208] WEEK 06 Solutions #2788
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 @@ | ||
| class Solution: | ||
| def maxArea(self, height: List[int]) -> int: | ||
| left, right = 0, len(height) - 1 | ||
|
|
||
| most_water_size = 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. ( ) 괄호는 붙이신 이유가 있을까요?
Member
Author
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_h, right_h = height[left], height[right] | ||
|
|
||
| current_size = (right-left) * min(left_h, right_h) | ||
| most_water_size = max(most_water_size, current_size) | ||
|
|
||
| if left_h < right_h: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
|
|
||
| return most_water_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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트라이로 단어를 저장하고 '.'를 재귀적으로 확장하는 표준 풀이이다. 개선 제안: 일반적으로 충분하지만, 메모리 사용량을 줄이려면 필요한 자식만 관리하는 구조를 고려해볼 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| class TrieNode: | ||
| def __init__(self): | ||
| self.children = {} | ||
| self.is_end = False | ||
|
|
||
|
|
||
| class WordDictionary: | ||
|
|
||
| def __init__(self): | ||
| self.root = TrieNode() | ||
|
|
||
| def addWord(self, word: str) -> None: | ||
| current = self.root | ||
|
|
||
| for char in word: | ||
| if char not in current.children: | ||
| current.children[char] = TrieNode() | ||
|
|
||
| current = current.children[char] | ||
|
|
||
| current.is_end = True | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| def dfs(index: int, node: TrieNode) -> bool: | ||
| # 패턴을 모두 확인했을 때 실제 단어의 끝인지 확인한다. | ||
| if index == len(word): | ||
| return node.is_end | ||
|
|
||
| char = word[index] | ||
|
|
||
| # '.'은 현재 노드의 모든 자식 문자와 대응될 수 있다. | ||
| if char == ".": | ||
| for child in node.children.values(): | ||
| if dfs(index + 1, child): | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| # 일반 문자는 해당 자식 노드만 확인한다. | ||
| child = node.children.get(char) | ||
|
|
||
| if child is None: | ||
| return False | ||
|
|
||
| return dfs(index + 1, child) | ||
|
|
||
| return dfs(0, self.root) |
|
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 풀이법이 있습니다! 시도해 보시죠!
Member
Author
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 단순 DP 방식으로 구현되어 있지만 시간 복잡도가 n^2으로 비효율적일 수 있다. 개선 제안: 가능하면 이분 탐색을 통한 O(n log n) 풀이로 변경하는 것을 고려해볼 만하다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| def lengthOfLIS(self, nums: List[int]) -> int: | ||
| # dp[i] : i를 마지막으로 했을 때 가장긴 증가 부분수열 | ||
| dp = [0] * len(nums) | ||
|
|
||
| for i in range(0, len(nums)): | ||
| dp[i] = 1 | ||
| for j in range(0, i): | ||
| if nums[j] < nums[i]: | ||
| dp[i] = max(dp[i], dp[j] + 1) | ||
|
|
||
| return max(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.
문제에서
Member
Author
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. b8faa50 다만 현재는 방문 여부를 표시하기 위해 혹시
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
Author
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,45 @@ | ||
| class Solution: | ||
| def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
| VISIT_NUM = 10000 | ||
|
|
||
| total = sum(map(lambda k: len(k), matrix)) | ||
| answer = [] | ||
|
|
||
| def isVisit(i, j): | ||
| return matrix[i][j] == VISIT_NUM | ||
|
|
||
| def setVisit(i, j): | ||
| matrix[i][j] = VISIT_NUM | ||
|
|
||
| def inRange(i, j): | ||
| if not (0<=i<len(matrix)): | ||
| return False | ||
| if not (0<=j<len(matrix[0])): | ||
| return False | ||
| return True | ||
|
|
||
| i, j = 0, 0 | ||
| answer.append(matrix[0][0]) | ||
| setVisit(0, 0) | ||
| while len(answer) < total: | ||
| while inRange(i, j+1) and not isVisit(i, j+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. in range 와 is visit 을 하나로 묶어서 함수로 추상화 해도 좋을 거 같습니다
Member
Author
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. 그러네요! 좋은 의견 감사드립니다!! |
||
| answer.append(matrix[i][j+1]) | ||
| setVisit(i, j+1) | ||
| j += 1 | ||
|
|
||
| while inRange(i+1, j) and not isVisit(i+1, j): | ||
| answer.append(matrix[i+1][j]) | ||
| setVisit(i+1, j) | ||
| i += 1 | ||
|
|
||
| while inRange(i, j-1) and not isVisit(i, j-1): | ||
| answer.append(matrix[i][j-1]) | ||
| setVisit(i, j-1) | ||
| j -= 1 | ||
|
|
||
| while inRange(i-1, j) and not isVisit(i-1, j): | ||
| answer.append(matrix[i-1][j]) | ||
| setVisit(i-1, j) | ||
| i -= 1 | ||
|
|
||
| return answer | ||
|
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,37 @@ | ||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| class Solution { | ||
| public boolean isValid(String s) { | ||
| Deque<Character> stack = new ArrayDeque<>(); | ||
| Map<Character, Character> mapping = new HashMap<>(); | ||
| mapping.put(')', '('); | ||
| mapping.put('}', '{'); | ||
| mapping.put(']', '['); | ||
|
Comment on lines
+9
to
+12
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. Map.of 사용해서 선언시점에 할당해도 좋을 거 같아요
Member
Author
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. 변경되지 않는 고정된 값이니 Map.of()를 사용해도 되겠네요. 알려주셔서 감사합니다! |
||
|
|
||
| for (Character c : s.toCharArray()) { | ||
| // 열린 괄호일 때, 스택에 push | ||
| if (c=='(' || c=='{' || c == '[') { | ||
| stack.push(c); | ||
| continue; | ||
| } | ||
|
|
||
| // 닫힌 괄호일 때, 스택에서 pop한 뒤, 비교 | ||
| // 스택이 비어있으면 탈락 | ||
| if (stack.isEmpty()) { | ||
| return false; | ||
| } | ||
| Character popedC = stack.pop(); | ||
| Character target = mapping.get(c); | ||
| // 같은 종류의 괄호가 아니면 탈락 | ||
| if (!target.equals(popedC)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // 마지막에 스택이 비어있어야 성공 | ||
| return stack.isEmpty(); | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.