-
-
Notifications
You must be signed in to change notification settings - Fork 341
[gcount85] WEEK 06 Solutions #2513
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
ad4b1cb
b2912ea
e34f9af
c1627a0
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,24 @@ | ||||||
| """ | ||||||
| # Approach | ||||||
| 투포인터로 양 끝에서 물의 양을 계산해나갑니다. | ||||||
| 더 낮은 height인 쪽의 포인터를 줄여나가는데, 높이가 같은 경우에는 양쪽 모두 포인터를 움직입니다. | ||||||
|
Member
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
|
||||||
|
|
||||||
| # Complexity | ||||||
| height의 길이가 N일 때 | ||||||
| - Time complexity: O(N) | ||||||
| - Space complexity: O(1) | ||||||
| """ | ||||||
|
|
||||||
|
|
||||||
| class Solution: | ||||||
| def maxArea(self, height: list[int]) -> int: | ||||||
| n = len(height) | ||||||
| left, right = 0, n - 1 | ||||||
| best = 0 | ||||||
| while left < right: | ||||||
| best = max(best, (right - left) * min(height[left], height[right])) | ||||||
| if height[left] > height[right]: | ||||||
| right -= 1 | ||||||
| else: | ||||||
| left += 1 | ||||||
| return best | ||||||
|
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,22 @@ | ||
| """ | ||
| # Approach | ||
| dp[n]은 nums[n] 위치까지 lis 길이입니다. | ||
|
|
||
| # Complexity | ||
| nums의 길이를 n이라고 할 때, | ||
| - Time complexity: 이중 반복문 O(N^2) | ||
| - Space complexity: dp 배열 O(N) | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def lengthOfLIS(self, nums: list[int]) -> int: | ||
| n = len(nums) | ||
| dp = [1] * n | ||
| answer = 1 | ||
| for i in range(1, n): | ||
| for j in range(i): | ||
| if nums[j] < nums[i]: | ||
| dp[i] = max(dp[j] + 1, dp[i]) | ||
| answer = max(answer, dp[i]) | ||
| 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,41 @@ | ||
| """ | ||
| # Approach | ||
| 상하좌우 경계값을 설정하고 matrix을 순회하며 경계값을 줄여나갑니다. | ||
|
|
||
| # Complexity | ||
| matrix 크기의 가로를 M, 세로를 N이라고 할 때 | ||
| - Time complexity: O(M*N) | ||
| - Space complexity: O(M*N) | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def spiralOrder(self, matrix: list[list[int]]) -> list[int]: | ||
| start_row, start_col = 0, 0 | ||
| end_row, end_col = len(matrix) - 1, len(matrix[0]) - 1 | ||
| output = [] | ||
|
|
||
| while start_row <= end_row and start_col <= end_col: | ||
| # 좌->우 | ||
| for col in range(start_col, end_col + 1): | ||
| output.append(matrix[start_row][col]) | ||
| start_row += 1 | ||
|
|
||
| # 상->하 | ||
| for row in range(start_row, end_row + 1): | ||
| output.append(matrix[row][end_col]) | ||
| end_col -= 1 | ||
|
|
||
| # 우->좌 | ||
| if start_row <= end_row: | ||
| for col in range(end_col, start_col - 1, -1): | ||
| output.append(matrix[end_row][col]) | ||
| end_row -= 1 | ||
|
|
||
| # 하->상 | ||
| if start_col <= end_col: | ||
| for row in range(end_row, start_row - 1, -1): | ||
| output.append(matrix[row][start_col]) | ||
| start_col += 1 | ||
|
|
||
| return output |
|
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,25 @@ | ||||||||||
| """ | ||||||||||
| # Approach | ||||||||||
| 스택을 이용하여 닫는 괄호가 나올 때 스택 top과 비교하여 짝이 맞으면 pop합니다. | ||||||||||
| 짝이 맞지 않으면 invalid, 최종적으로 스택이 비어있지 않으면 invalid 합니다. | ||||||||||
|
|
||||||||||
| # Complexity | ||||||||||
| s의 길이를 N이라고 할 때 | ||||||||||
| - Time complexity: O(N) | ||||||||||
| - Space complexity: O(N) | ||||||||||
| """ | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class Solution: | ||||||||||
| def isValid(self, s: str) -> bool: | ||||||||||
| stack = [] | ||||||||||
| brackets = {"(": ")", "{": "}", "[": "]"} | ||||||||||
| for b in s: | ||||||||||
| if b in brackets: | ||||||||||
| stack.append(b) | ||||||||||
| continue | ||||||||||
| if not stack or brackets[stack.pop()] != b: | ||||||||||
| return False | ||||||||||
| if not stack: | ||||||||||
| return True | ||||||||||
| return False | ||||||||||
|
Comment on lines
+23
to
+25
Member
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. 요렇게 짜시면 좀 더 Pythonic한 코드가 되지 않을까 생각해보았습니다. 별로 중요하지 않은 코멘트입니다.
Suggested change
|
||||||||||
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.
🏷️ 알고리즘 패턴 분석