Problem:
https://leetcode.com/problems/container-with-most-water/
Solution:
Of course we gonna think about brute force, the idea is pretty straight forward. For each pair, we calculate there container then keep the biggest one.
So how can we improve the algorithm. We can have two pointer.
public class Solution {
public int maxArea(int[] height) {
if(height == null || height.length == 0 || height.length == 1){
return 0;
}
int i = 0;
int j = height.length-1;
int max = Math.min(height[i], height[j]) *(j - i);
while(i < j){
if(height[i] < height[j]){
max = Math.max(height[i]*(j-i),max);
i++;
}else{
max = Math.max(height[j]*(j-i),max);
j--;
}
}
return max;
}
}