Skip to content

[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 1 commit into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions container-with-most-water/sounmind.js

This file was deleted.

62 changes: 62 additions & 0 deletions container-with-most-water/sounmind.py
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

명확한 변수명을 통한 코드 작성 맥락과 함께 상세한 설명 덕분에 코드 이해가 훨씬 수월했습니다. 👏