-
Notifications
You must be signed in to change notification settings - Fork 126
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
[mangodm-web] Week 06 Solutions #471
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
""" | ||
- Idea: 배열의 양쪽 끝에서 시작하는 두 포인터(left, right)를 이용해 두 선 사이의 최대 영역을 구한다. 둘 중, 높이가 낮은 쪽의 포인터는 중앙 쪽으로 이동시킨다. | ||
- Time Complexity: O(n), n은 주어진 배열(height)의 길이. | ||
- Space Complexity: O(1), 추가 공간은 사용하지 않는다. | ||
""" | ||
left, right = 0, len(height) - 1 | ||
result = 0 | ||
|
||
while left < right: | ||
current_width = right - left | ||
current_height = min(height[left], height[right]) | ||
result = max(result, current_width * current_height) | ||
|
||
if height[left] < height[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
""" | ||
- Idea: 네 개의 포인터(left, right, top, bottom)를 활용하여 행렬의 바깥쪽부터 안쪽으로 순회한다. | ||
- left, right: 행렬의 왼쪽과 오른쪽 끝. 순회하면서 점점 좁혀진다. | ||
- top, bottom: 행렬의 위쪽과 아래쪽. 순회하면서 점점 좁혀진다. | ||
- Time Complexity: O(m*n), m, n은 각각 주어진 행렬(matrix)의 행과 열의 개수. 행렬의 모든 요소를 한번씩 접근한다. | ||
- Space Complexity: O(1), 결과 리스트(result)를 제외하고 포인터를 위한 상수 크기의 변수 이외의 추가 공간은 사용하지 않는다. | ||
""" | ||
result = [] | ||
left, right = 0, len(matrix[0]) | ||
top, bottom = 0, len(matrix) | ||
|
||
while left < right and top < bottom: | ||
for i in range(left, right): | ||
result.append(matrix[top][i]) | ||
top += 1 | ||
|
||
for i in range(top, bottom): | ||
result.append(matrix[i][right - 1]) | ||
right -= 1 | ||
|
||
if not (left < right and top < bottom): | ||
break | ||
|
||
for i in range(right - 1, left - 1, -1): | ||
result.append(matrix[bottom - 1][i]) | ||
bottom -= 1 | ||
|
||
for i in range(bottom - 1, top - 1, -1): | ||
result.append(matrix[i][left]) | ||
left += 1 | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
""" | ||
- Idea: 주어진 문자열을 순회하면서 여는 괄호는 스택에 넣고, 닫는 괄호는 스택의 최상단 요소와 매칭되는지 확인한다. | ||
- Time Complexity: O(n), n은 주어진 문자열의 길이. 모든 문자를 한번씩은 순회한다. | ||
- Space Complexity: O(n), 주어진 문자열이 모두 여는 괄호일 경우 스택에 저장된다. | ||
""" | ||
bracket_map = {"(": ")", "[": "]", "{": "}"} | ||
stack = [] | ||
|
||
for char in s: | ||
if char in bracket_map: | ||
stack.append(char) | ||
elif not stack or bracket_map[stack.pop()] != char: | ||
return False | ||
|
||
return not stack |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
오 정말 깔끔한 풀이네요. 감탄하고 갑니다! 👍