Skip to content

Commit 2b439e3

Browse files
committed
Container with most water
1 parent 93d387f commit 2b439e3

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
### Given an array of line heights, find the two lines that form the container holding the most water.
2+
3+
4+
fn max_area(heights: List[Int]) -> Int:
5+
# If there are fewer than 2 lines, no container can be formed
6+
if len(heights) < 2:
7+
return 0
8+
9+
left, right = 0, len(heights) - 1
10+
11+
max_area = 0
12+
13+
while left < right:
14+
# Height of container is limited by the shorter of the two lines
15+
min_height = min(heights[left], heights[right])
16+
17+
# Calculate area formed between the two lines and update max_area if it's larger
18+
max_area = max(max_area, (right - left) * min_height)
19+
20+
# Move the pointer that's at the shorter line inward to potentially find a taller line
21+
# This can potentially increase the area despite reducing the width
22+
if heights[left] <= heights[right]:
23+
left += 1
24+
else:
25+
right -= 1
26+
27+
return max_area
28+
29+
30+
from testing import assert_equal
31+
32+
33+
fn main():
34+
heights = List(1, 8, 6, 2, 5, 4, 8, 3, 7)
35+
mx_area = max_area(heights)
36+
assert_equal(mx_area, 49, "Assertion failed")
37+
38+
heights = List(1, 1)
39+
mx_area = max_area(heights)
40+
assert_equal(mx_area, 1, "Assertion failed")

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
- [ Convert String to Signed Integer](atoi.md) ➔ Implement the atoi(string s) function, which converts a string to a 64-bit signed integer.
1919
- [ Remove Duplicate](remove_duplicates_sorted_arr.md) ➔ Remove duplicates from sorted array.
2020
- [ Merge Sorted Array In Place](merge_shorted_arr_in_place.md) ➔ A has extra space to hold B. Merge B into A as one sorted array.
21+
- [ Container With Most Water](water_container_max_area.md) ➔ Given an array of line heights, find the two lines that form the container holding the most water.

docs/water_container_max_area.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
### Given an array of line heights, find the two lines that form the container holding the most water.
2+
3+
```python
4+
5+
fn max_area(heights: List[Int]) -> Int:
6+
# If there are fewer than 2 lines, no container can be formed
7+
if len(heights) < 2:
8+
return 0
9+
10+
left, right = 0, len(heights) - 1
11+
12+
max_area = 0
13+
14+
while left < right:
15+
# Height of container is limited by the shorter of the two lines
16+
min_height = min(heights[left], heights[right])
17+
18+
# Calculate area formed between the two lines and update max_area if it's larger
19+
max_area = max(max_area, (right - left) * min_height)
20+
21+
# Move the pointer that's at the shorter line inward to potentially find a taller line
22+
# This can potentially increase the area despite reducing the width
23+
if heights[left] <= heights[right]:
24+
left += 1
25+
else:
26+
right -= 1
27+
28+
return max_area
29+
30+
31+
from testing import assert_equal
32+
33+
34+
fn main():
35+
heights = List(1, 8, 6, 2, 5, 4, 8, 3, 7)
36+
mx_area = max_area(heights)
37+
assert_equal(mx_area, 49, "Assertion failed")
38+
39+
heights = List(1, 1)
40+
mx_area = max_area(heights)
41+
assert_equal(mx_area, 1, "Assertion failed")
42+
43+
```
44+
[Source code](https://github.com/ratulb/mojo_programming/blob/main/codes/water_container_max_area.mojo)
45+

0 commit comments

Comments
 (0)