java: add «11. Container With Most Water»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
66f8d84892
commit
23cd6ac490
1 changed files with 18 additions and 0 deletions
18
java/container-with-most-water.java
Normal file
18
java/container-with-most-water.java
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
class Solution {
|
||||||
|
public int maxArea(int[] height) {
|
||||||
|
int foundMax = 0;
|
||||||
|
|
||||||
|
int l = 0, r = height.length - 1;
|
||||||
|
while (l < r) {
|
||||||
|
foundMax = Math.max(foundMax, (r - l) * Math.min(height[l], height[r]));
|
||||||
|
|
||||||
|
if (height[l] < height[r]) {
|
||||||
|
++l;
|
||||||
|
} else {
|
||||||
|
--r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundMax;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue