-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerWithMostWater.java
More file actions
33 lines (32 loc) · 1001 Bytes
/
ContainerWithMostWater.java
File metadata and controls
33 lines (32 loc) · 1001 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
26
27
28
29
30
31
32
33
package com.cier.solution.array;
/**
* https://leetcode-cn.com/problems/container-with-most-water/description/
*/
public class ContainerWithMostWater {
/**
* Runtime: 3 ms, faster than 54.04% of Java online submissions for Container With Most Water.
* Memory Usage: 39.4 MB, less than 96.47% of Java online submissions for Container With Most Water.
* @param height
* @return
*/
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int ret = min(height[left], height[right]) * (right - left);
while (left < right) {
if (height[left] < height[right]) {
left++;
} else {
right--;
}
ret = max(ret, min(height[left],height[right]) * (right - left));
}
return ret;
}
public int max(int a, int b) {
return a > b ? a : b;
}
public int min(int a, int b) {
return a < b ? a : b;
}
}