Skip to content

Commit 643b007

Browse files
authored
Merge pull request #363 from MoigeMatino/container-with-most-water
feat: container with most water
2 parents fa248c3 + 68e223b commit 643b007

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## **Problem Statement**
2+
3+
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])`.
4+
5+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
6+
7+
Return the maximum amount of water a container can store.
8+
9+
Notice that you may not slant the container.
10+
11+
### Example 1
12+
Input: height = [1,8,6,2,5,4,8,3,7]
13+
Output: 49
14+
15+
Explanation: The vertical lines are represented by array [1,8,6,2,5,4,8,3,7].
16+
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.
17+
To get the width: 8 - 1 = 7
18+
To get height = min(8, 7) = 7
19+
To get area = height * width = 7 * 7 = 49
20+
21+
### Example 2
22+
Input: height = [1,1]
23+
Output: 1

arrays_and_strings/container_with_most_water/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
def max_area(height: list[int]) -> int:
2+
"""
3+
Calculate the maximum area of water a container can store, formed by two lines
4+
from the given list of heights and the x-axis.
5+
6+
Parameters:
7+
height (list[int]): A list of integers representing the heights of vertical lines.
8+
9+
Returns:
10+
int: The maximum area of water that can be contained.
11+
12+
"""
13+
left = 0
14+
right = len(height) - 1
15+
max_area = 0
16+
17+
while left < right:
18+
# Calculate the height and width of the current container
19+
curr_height = min(height[left], height[right])
20+
curr_width = right - left
21+
curr_area = curr_height * curr_width
22+
23+
# Update max_area if the current area is larger
24+
max_area = max(curr_area, max_area)
25+
26+
# Move the pointer pointing to the shorter line
27+
if height[left] < height[right]:
28+
left += 1
29+
else:
30+
right -= 1
31+
32+
return max_area
33+
34+
# Approach and Reasoning:
35+
# -----------------------
36+
# - We use the two-pointer technique to solve this problem efficiently.
37+
# - Initialize two pointers, `left` at the start and `right` at the end of the list.
38+
# - The width of the container is the distance between the two pointers.
39+
# - The height of the container is determined by the shorter of the two lines at the pointers.
40+
# - Calculate the area for the current pair of lines and update `max_area` if this area is larger.
41+
# - Move the pointer pointing to the shorter line inward to potentially find a taller line,
42+
# which might result in a larger area.
43+
# - Repeat the process until the two pointers meet.
44+
45+
# Time Complexity:
46+
# ----------------
47+
# - The time complexity of this approach is O(n), where n is the number of elements in the `height` list.
48+
# - This is because each element is processed at most once as the pointers move towards each other.
49+
50+
# Space Complexity:
51+
# -----------------
52+
# - The space complexity is O(1) because we are using a constant amount of extra space,
53+
# regardless of the input size.
54+

0 commit comments

Comments
 (0)