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
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0011_container_with_most_water/readme.md
+57-1Lines changed: 57 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,4 +30,60 @@ Return _the maximum amount of water a container can store_.
30
30
31
31
*`n == height.length`
32
32
* <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
+
publicclassSolution {
51
+
publicintmaxArea(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]);
0 commit comments