Skip to content

Commit 908f16a

Browse files
committed
feat: solved 11
1 parent 775c9b4 commit 908f16a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fghpdf.ContainerWithMostWater;
2+
3+
/**
4+
* @author fghpdf
5+
* @date 2019/11/10
6+
* https://leetcode.com/problems/container-with-most-water/submissions/
7+
*
8+
* because the shorter vertical line decide the area
9+
* so when you find left < right, area = left * length
10+
* the bigger area may be appear left, it can make right line to take effect
11+
**/
12+
public class Solution {
13+
public int maxArea(int[] height) {
14+
int result = 0;
15+
16+
int left = 0;
17+
int right = height.length - 1;
18+
19+
while (left < right ) {
20+
result = Math.max(result, (right - left) * Math.min(height[left], height[right]));
21+
if (height[left] < height[right]) {
22+
left++;
23+
} else {
24+
right--;
25+
}
26+
}
27+
return result;
28+
}
29+
30+
public static void main(String[] args) {
31+
Solution sol = new Solution();
32+
int[] a = {1,8,6,2,5,4,8,3,7};
33+
System.out.println(sol.maxArea(a));
34+
}
35+
}

0 commit comments

Comments
 (0)