-
-
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
Open
gcount85
wants to merge
6
commits into
DaleStudy:main
Choose a base branch
from
gcount85:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad4b1cb
implement maxArea function to calculate maximum water container area
gcount85 b2912ea
add lengthOfLIS function to calculate the length of the longest incre…
gcount85 e34f9af
add spiralOrder function to return elements of a matrix in spiral order
gcount85 c1627a0
add isValid function to validate parentheses using a stack
gcount85 416d65b
Update container-with-most-water/gcount85.py
gcount85 177774e
Update valid-parentheses/gcount85.py
gcount85 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 hidden or 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 @@ | ||
| """ | ||
| # Approach | ||
| 투포인터로 양 끝에서 물의 양을 계산해나갑니다. | ||
| 더 낮은 height인 쪽의 포인터를 줄여나가는데, 높이가 같은 경우에는 왼쪽 포인터만 움직입니다. | ||
|
|
||
| # 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. 🏷️ 알고리즘 패턴 분석
|
This file contains hidden or 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,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. 🏷️ 알고리즘 패턴 분석
|
This file contains hidden or 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,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 |
This file contains hidden or 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,23 @@ | ||
| """ | ||
| # 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 | ||
| return not stack |
Oops, something went wrong.
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.
🏷️ 알고리즘 패턴 분석