Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8baabab

Browse files
11. Container With Most Water
1 parent b112725 commit 8baabab

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'''
2+
11. Container With Most Water
3+
4+
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the line i is at (i, 0) and (i, height[i]). Find two lines, which together with the x-axis form a container, such that the container contains the most water.
5+
You may not slant the container and n is at least 2.
6+
Return the maximum amount of water a container can store.
7+
The answer is guaranteed to be less than or equal to 10^4.
8+
9+
Example 1:
10+
Input: height = [1,8,6,2,5,4,8,3,7]
11+
Output: 49
12+
Explanation: The above 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.
13+
14+
Example 2:
15+
Input: height = [1,1]
16+
Output: 1
17+
Explanation: The above vertical lines are represented by array [1,1]. In this case, the max area of water the container can contain is 1.
18+
'''
19+
20+
#Note:
21+
'''
22+
- The container can only hold water if the two lines are not the same.
23+
- The area of water that can be contained between two lines is determined by the shorter line and the distance between the two lines.
24+
- The maximum area is the maximum of all possible areas that can be formed by any two lines.
25+
26+
Why we chose j-i as width?
27+
- The width of the container is determined by the distance between the two lines.
28+
29+
Why we chose min(height[i], height[j]) as height?
30+
- The height of the container is determined by the shorter of the two lines, as water cannot overflow the shorter line.
31+
32+
- The area of the container is then calculated as width * height, which is (j - i) * min(height[i], height[j]).
33+
'''
34+
35+
36+
# Brute Force
37+
# Time Complexity: O(n^2)
38+
# Space Complexity: O(1)
39+
40+
'''
41+
So in the brute force approach, we will iterate through all the pairs of lines and calculate the area of water that can be contained between them.
42+
The area is calculated as min(height[i], height[j]) * (j - i), where i and j are the indices of the two lines.
43+
We will keep track of the maximum area found so far and return it at the end.
44+
'''
45+
46+
class Solution:
47+
def maxArea(self, height):
48+
max_area=0
49+
50+
for i in range(len(height)):
51+
for j in range(i+1, len(height)):
52+
new_h=min(height[i],height[j])
53+
width = j-i
54+
area= new_h*width
55+
max_area=max(area,max_area)
56+
57+
return max_area
58+
59+
# Two Pointers
60+
# Time Complexity: O(n)
61+
# Space Complexity: O(1)
62+
63+
'''
64+
The two pointers approach is a more efficient way to solve the problem.
65+
In this approach, we start with two pointers, one at the beginning of the array and one at the end.
66+
We calculate the area of water that can be contained between the two lines at the pointers and update the maximum area found so far.
67+
Then we move the pointer pointing to the shorter line towards the other pointer. I mean if left wall is shorter than right wall, we move left pointer to the right.
68+
If the right wall is shorter, we move the right pointer to the left.
69+
This is because the area is limited by the shorter line, so moving the pointer pointing to the shorter line may lead to a larger area.
70+
We repeat this process until the two pointers meet.
71+
'''
72+
class Solution:
73+
def maxArea(self, heights):
74+
max_area=0
75+
76+
l=0
77+
r=len(heights)-1
78+
79+
while l<r:
80+
height=min(heights[l],heights[r])
81+
width=r-l
82+
area=height*width
83+
max_area=max(area,max_area)
84+
if heights[l]<heights[r]:
85+
l+=1
86+
else:
87+
r-=1
88+
89+
return max_area
90+
91+
92+
93+
sol = Solution()
94+
heights = [1,8,6,2,5,4,8,3,7]
95+
print(sol.maxArea(heights))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /