-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_11.java
More file actions
25 lines (24 loc) · 841 Bytes
/
Copy pathproblem_11.java
File metadata and controls
25 lines (24 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
11. Container With Most Water
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. */
class Solution {
public int maxArea(int[] height) {
int left=0, right=height.length-1, area=0,width,h;
while(left<right){
width=right-left;
h=Math.min(height[left],height[right]);
if(area<width*h){
area=width*h;
}
if(height[left]<height[right]){
left++;
}
else{
right--;
}
}
return area;
}
}