Wednesday, March 30, 2022

Leet Code 11. Container With Most Water


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

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...