Container With Most Water
LeetCode https://leetcode.cn/problems/container-with-most-water/
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int maxArea(int[] height) {
int i = 0;
int j = height.length - 1;
int globalMax = 0;
while (i < j) {
globalMax = height[i] < height[j] ?
Math.max(globalMax, (j - i) * height[i++]) :
Math.max(globalMax, (j - i) * height[j--]);
}
return globalMax;
}
}
Complexity
- Time = O(n)
- Space = O(1)
This post is licensed under CC BY 4.0 by the author.