https://leetcode.com/problems/container-with-most-water/
Example 1:

Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
package pep.Day52;
public class LeetCode_11_Container_With_Most_Water {
public static int maxArea(int[] height) {
int start = 0;
int end = height.length - 1;
int maxArea = 0;
while (start < end) {
maxArea = Math.max(maxArea, (end - start) * Math.min(height[start], height[end]));
// this means that start is at wrong position
if (height[start] < height[end])
start++;
else
end--;
}
return maxArea;
}
}

No comments:
Post a Comment