Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/TwoPointers/ContainerWithMostWater_11/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package TwoPointers.ContainerWithMostWater_11;

public class Main {
public static void main(String[] args) {
Solution s = new Solution();

int[] input = {1,8,6,2,5,4,8,3,7};
System.out.println(s.getContainerWithMostWater(input));
}
}
39 changes: 39 additions & 0 deletions src/TwoPointers/ContainerWithMostWater_11/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package TwoPointers.ContainerWithMostWater_11;

import java.lang.Math;

/**
* 11. Container With Most Water
*
* Given n 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 the line i is at (i, ai) and (i, 0).
* Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
*
* Input: heights = [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, the container can contain is 49.

leetcode link : https://leetcode.com/problems/container-with-most-water/
* */

class Solution {
public int getContainerWithMostWater(int[] heights) {
int l = 0;
int r = heights.length - 1;
int maxArea = 0;

while (l < r) {
int min = Math.min(heights[l], heights[r]);
int curr = min * (r - l);
maxArea = Math.max(maxArea, curr);

if (heights[r] == heights[l]) {
l++;
r--;
}
else if (heights[l] < heights[r]) l++;
else r--;
}
return maxArea;
}
}