Skip to content

Commit 94ee512

Browse files
committed
Added : Container with Most Water
1 parent 9bd3b33 commit 94ee512

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
2+
3+
Note: You may not slant the container and n is at least 2.
4+
5+
6+
7+
8+
9+
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 (blue section) the container can contain is 49.
10+
11+
12+
13+
Example:
14+
15+
Input: [1,8,6,2,5,4,8,3,7]
16+
Output: 49
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
@lc id : 11
3+
@problem : Container With Most Water
4+
@author : rohit
5+
@url : https://leetcode.com/problems/container-with-most-water/
6+
@difficulty : medium
7+
*/
8+
9+
class Solution {
10+
public int maxArea(int[] height) {
11+
int max = Integer.MIN_VALUE;
12+
int i = 0;
13+
int j = height.length - 1;
14+
15+
while(i < j){
16+
int min = Math.min(height[i], height[j]);
17+
max = Math.max(max, min * (j - i));
18+
19+
if(height[i] < height[j])
20+
i++;
21+
else j--;
22+
}
23+
24+
return max;
25+
}
26+
}

0 commit comments

Comments
 (0)