011-container-with-most-water
Question
https://leetcode.com/problems/container-with-most-water/description/
Given 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.
Note: You may not slant the container and n is at least 2.
Example:
Thought Process
- Brute Force
- Start with each vertical line as the container's left boundary
- Pick every one of the remaining vertical line as the right boundary
- Volume = (x2 - x1) * min(y2, y1)
- Time complexity is O(n)
- Space complexity is O(1)
- Two pointers
- Start with pointer at left and right, this could potentially by our largest container because the widest length
- We compute the volume same way as before
- Because only the larger height will be most useful in getting larger volume, we shrink the side that has lesser height
- Time complexity is O(n)
- Space complexity is O(1)
Solution
class Solution {
public int maxArea(int[] height) {
int maxArea = 0, l = 0, r = height.length - 1;
while (l < r) {
maxArea = Math.max(maxArea, Math.min(height[l], height[r]) * (r - l));
if (height[l] > height[r]) r--;
else l++;
}
return maxArea;
}
}