Skip to content

Commit 9f5fddc

Browse files
authored
Update readme with solution details for Container With Most Water
Added a detailed explanation and implementation for the Container With Most Water problem.
1 parent fbd0f5d commit 9f5fddc

File tree

1 file changed

+57
-1
lines changed
  • src/main/java/g0001_0100/s0011_container_with_most_water

1 file changed

+57
-1
lines changed

src/main/java/g0001_0100/s0011_container_with_most_water/readme.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,60 @@ Return _the maximum amount of water a container can store_.
3030

3131
* `n == height.length`
3232
* <code>2 <= n <= 10<sup>5</sup></code>
33-
* <code>0 <= height[i] <= 10<sup>4</sup></code>
33+
* <code>0 <= height[i] <= 10<sup>4</sup></code
34+
35+
To solve the Container With Most Water problem in Java using a `Solution` class, we'll follow these steps:
36+
37+
1. Define a `Solution` class with a method named `maxArea` that takes an array of integers `height` as input and returns the maximum area of water that can be contained.
38+
2. Initialize two pointers, `left` pointing to the start of the array and `right` pointing to the end of the array.
39+
3. Initialize a variable `maxArea` to store the maximum area encountered so far, initially set to 0.
40+
4. Iterate while `left` is less than `right`.
41+
5. Calculate the current area using the formula: `(right - left) * min(height[left], height[right])`.
42+
6. Update `maxArea` if the current area is greater than `maxArea`.
43+
7. Move the pointer pointing to the smaller height towards the other pointer. If `height[left] < height[right]`, increment `left`, otherwise decrement `right`.
44+
8. Continue the iteration until `left` becomes greater than or equal to `right`.
45+
9. Return `maxArea`.
46+
47+
Here's the implementation:
48+
49+
```java
50+
public class Solution {
51+
public int maxArea(int[] height) {
52+
int left = 0;
53+
int right = height.length - 1;
54+
int maxArea = 0;
55+
56+
while (left < right) {
57+
int currentArea = (right - left) * Math.min(height[left], height[right]);
58+
maxArea = Math.max(maxArea, currentArea);
59+
60+
if (height[left] < height[right]) {
61+
left++;
62+
} else {
63+
right--;
64+
}
65+
}
66+
67+
return maxArea;
68+
}
69+
70+
public static void main(String[] args) {
71+
Solution solution = new Solution();
72+
73+
// Test cases
74+
int[] height1 = {1, 8, 6, 2, 5, 4, 8, 3, 7};
75+
System.out.println("Example 1 Output: " + solution.maxArea(height1));
76+
77+
int[] height2 = {1, 1};
78+
System.out.println("Example 2 Output: " + solution.maxArea(height2));
79+
80+
int[] height3 = {4, 3, 2, 1, 4};
81+
System.out.println("Example 3 Output: " + solution.maxArea(height3));
82+
83+
int[] height4 = {1, 2, 1};
84+
System.out.println("Example 4 Output: " + solution.maxArea(height4));
85+
}
86+
}
87+
```
88+
89+
This implementation provides a solution to the Container With Most Water problem in Java.

0 commit comments

Comments
 (0)