Guest User

Untitled

a guest
Oct 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class ContainerWithMostWater {
  2. /*
  3. Initially we can assume the result is 0, then we scan from both sides with two pointers.
  4. If leftHeight < rightHeight, move the left pointer right and find a value that is greater than leftHeight.
  5. Similarly, if leftHeight > rightHeight, move the right pointer left and find a value that is greater than rightHeight.
  6. Additionally, keep tracking the max value.
  7. */
  8. public int maxArea(int[] height) {
  9. if(height == null || height.length < 2){
  10. return 0;
  11. }
  12. int n = height.length;
  13. int area = 0;
  14. int x1 = 0;
  15. int x2 = n-1;
  16. while(x1<x2){
  17. int areaTemp = Math.min(height[x1],height[x2])*(x2-x1);
  18. area = Math.max(area,areaTemp);
  19. if(height[x1]<height[x2]){
  20. x1++;
  21. } else {
  22. x2--;
  23. }
  24. }
  25. return area;
  26. }
  27. }
Add Comment
Please, Sign In to add comment