Skip to content

Commit 2fce3cc

Browse files
committed
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. (java)
1 parent 863b5ae commit 2fce3cc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

maxarea.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class maxarea {
2+
public int maxArea(int[] height) {
3+
int m = 0;
4+
int left = 0;
5+
int right = height.length - 1;
6+
while (left < right) {
7+
int h = Math.min(height[left], height[right]);
8+
int w = right - left;
9+
int area = h * w;
10+
m = Math.max(m, area);
11+
12+
if (height[left] < height[right]) {
13+
left++;
14+
} else {
15+
right--;
16+
}
17+
}
18+
return m;
19+
20+
}
21+
}

0 commit comments

Comments
 (0)