-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sounmind] WEEK 6 SOLUTIONS #1440
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
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 was deleted.
Oops, something went wrong.
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,62 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def maxArea(self, heights: List[int]) -> int: | ||
""" | ||
Find the maximum area that can form a container with the most water. | ||
|
||
Problem: Given n non-negative integers representing heights of vertical lines, | ||
find two lines that together with the x-axis forms a container that holds the most water. | ||
|
||
Approach: | ||
- Use two pointers technique starting from both ends of the array | ||
- Calculate area at each step and keep track of maximum | ||
- Move the pointer with smaller height inward (since it limits the container height) | ||
|
||
Proof of correctness: | ||
- Every time we move a pointer, the width decreases by 1. | ||
- Why we move the pointer with smaller height: | ||
1) If we keep the smaller height and move the taller pointer instead, | ||
the width decreases but the height remains limited by the smaller value, | ||
so the area will always decrease. | ||
2) However, if we move the pointer with smaller height, we might find a taller line. | ||
In this case, even though width decreases, height might increase enough | ||
to create a larger area. | ||
- This way, we examine all possible combinations that could give us the maximum area. | ||
- We only explore combinations that have the potential to create an area larger | ||
than the maximum we've found so far. | ||
|
||
Time Complexity: O(n) where n is the length of heights array | ||
Space Complexity: O(1) using constant extra space | ||
|
||
Args: | ||
heights: List of heights of the vertical lines | ||
|
||
Returns: | ||
Maximum water area that can be contained | ||
""" | ||
max_area = 0 # Initialize the maximum area to 0 | ||
left, right = 0, len(heights) - 1 # Start with leftmost and rightmost positions | ||
|
||
# Continue until the pointers meet | ||
while left < right: | ||
# Calculate width between current lines (difference in positions) | ||
width = right - left | ||
|
||
# Height is limited by the shorter line | ||
current_height = min(heights[left], heights[right]) | ||
|
||
# Calculate current area and update max_area if larger | ||
current_area = width * current_height | ||
max_area = max(max_area, current_area) | ||
|
||
# Move the pointer with smaller height inward | ||
# (Moving the smaller one gives potential for larger area since | ||
# width will decrease but height might increase) | ||
if heights[left] < heights[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return max_area | ||
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.
명확한 변수명을 통한 코드 작성 맥락과 함께 상세한 설명 덕분에 코드 이해가 훨씬 수월했습니다. 👏