You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
classSolution:
47
+
defmaxArea(self, height):
48
+
max_area=0
49
+
50
+
foriinrange(len(height)):
51
+
forjinrange(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
+
returnmax_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.
0 commit comments