Skip to content

feat: container with most water #363

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 3 commits into from
Oct 27, 2024
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: 23 additions & 0 deletions arrays_and_strings/container_with_most_water/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## **Problem Statement**

You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the `ith` line are `(i, 0)` and `(i, height[i])`.

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

### Example 1
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49

Explanation: The vertical lines are represented by array [1,8,6,2,5,4,8,3,7].
In this case, the max area of water the container can contain is 49. Derived from (1, height[1]) and (8, height[8]) which gives us the heights; h1 = 8, h2 = 7.
To get the width: 8 - 1 = 7
To get height = min(8, 7) = 7
To get area = height * width = 7 * 7 = 49

### Example 2
Input: height = [1,1]
Output: 1
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
def max_area(height: list[int]) -> int:
"""
Calculate the maximum area of water a container can store, formed by two lines
from the given list of heights and the x-axis.

Parameters:
height (list[int]): A list of integers representing the heights of vertical lines.

Returns:
int: The maximum area of water that can be contained.

"""
left = 0
right = len(height) - 1
max_area = 0

while left < right:
# Calculate the height and width of the current container
curr_height = min(height[left], height[right])
curr_width = right - left
curr_area = curr_height * curr_width

# Update max_area if the current area is larger
max_area = max(curr_area, max_area)

# Move the pointer pointing to the shorter line
if height[left] < height[right]:
left += 1
else:
right -= 1

return max_area

# Approach and Reasoning:
# -----------------------
# - We use the two-pointer technique to solve this problem efficiently.
# - Initialize two pointers, `left` at the start and `right` at the end of the list.
# - The width of the container is the distance between the two pointers.
# - The height of the container is determined by the shorter of the two lines at the pointers.
# - Calculate the area for the current pair of lines and update `max_area` if this area is larger.
# - Move the pointer pointing to the shorter line inward to potentially find a taller line,
# which might result in a larger area.
# - Repeat the process until the two pointers meet.

# Time Complexity:
# ----------------
# - The time complexity of this approach is O(n), where n is the number of elements in the `height` list.
# - This is because each element is processed at most once as the pointers move towards each other.

# Space Complexity:
# -----------------
# - The space complexity is O(1) because we are using a constant amount of extra space,
# regardless of the input size.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy