package no011.container.with.most.water;

import java.util.HashMap;

public class Solution {

  private static class Range {

    int min;
    int max;

    Range(int dot) {
      min = dot;
      max = dot;
    }

    void setRange(int dot) {

      if (max < dot) {
        max = dot;
      }
      if (min > dot) {
        min = dot;
      }
    }
  }

  public static int maxArea(int[] height) {

    int longest = height.length;
    int highest = 0;
    int maxArea = 0;

    HashMap<Integer, Range> map = new HashMap<>();
    for (int i = 0; i < longest; i++) {
      if(highest < height[i]) {
        highest = height[i];
      }
      if (map.containsKey(height[i])) {
        Range range = map.get(height[i]);
        range.setRange(i);
      } else {
        Range range = new Range(i);
        map.put(height[i], range);
      }
    }

    for (int i = longest; i > 0; i--) {
      int min = 0;
      int max = 0;
      boolean flag = true;
      for(int j = highest; j > 0; j--) {
        if( i * j < maxArea ) {
          break;
        }
        if(map.containsKey(j)) {
          Range range = map.get(j);
          if(min == 0 && max == 0 && flag) {
            min = range.min;
            max = range.max;
            flag = false;
          } else {
            if(range.min < min) {
              min = range.min;
            }
            if(range.max > max) {
              max = range.max;
            }
          }

          int length = max - min;
          int area = j * length;
          if(area > maxArea) {
            maxArea = area;
          }
        }
      }
    }

    return maxArea;
  }

  public static void main(String[] args) {
    //System.out.println(maxArea(new int[]{1,2,3,4,1}));
    System.out.println(maxArea(new int[]{2,1}));
    //System.out.println(maxArea(new int[]{1,2,3,4,1,5}));
  }
}

@@ -39,874 +39,531 @@
 r;%0A%0A
-import java.util.HashMap;%0A%0Apublic class Solution %7B%0A%0A  private static class Range %7B%0A%0A    int min;%0A    int max;%0A%0A    Range(int dot) %7B%0A      min = dot;%0A      max = dot;%0A    %7D%0A%0A    void setRange(int dot) %7B%0A%0A      if (max %3C dot) %7B%0A        max = dot;%0A      %7D%0A      if (min %3E dot) %7B%0A        min = dot;%0A      %7D%0A    %7D%0A  %7D%0A%0A  public static int maxArea(int%5B%5D height) %7B%0A%0A    int longest = height.length;%0A    int highest = 0;%0A    int maxArea = 0;%0A%0A    HashMap%3CInteger, Range%3E map = new HashMap%3C%3E();%0A    for (int i = 0; i %3C longest; i++) %7B%0A      if(highest %3C height%5Bi%5D) %7B%0A        highest = height%5Bi%5D;%0A      %7D
+public class Solution %7B%0A%0A  /**%0A   * Retrieve max area by supplied integer array of height.%0A   *%0A   * Compare the very left point to the very right point at each moment and save the lower height to%0A   * retrieve area between left and right. Move the bigger side point to the middle point side of%0A   * both and set a bigger value as a current max area between the existed max area and the saved%0A   * area.
 %0A   
+*%0A
    
-if (map.containsKey(height%5Bi%5D)) %7B%0A        Range range = map.get(height%5Bi%5D);%0A        range.setRange(i);%0A      %7D else %7B%0A        Range range = new Range(i);%0A        map.put(height%5Bi%5D, range);%0A      %7D%0A    %7D%0A%0A    for (int i = longest; i %3E 0; i--) %7B%0A      int min = 0;%0A  
+* @param height The array of height%0A   * @return A max area%0A   */%0A  public static int maxArea(int%5B%5D height) %7B%0A%0A
     
@@ -569,16 +569,20 @@
  int max
+Area
  = 0;%0A  
@@ -587,333 +587,234 @@
     
-  boolean flag = true;%0A      for(int j
+int left = 0;%0A    int right
  = h
+e
 igh
-est; j %3E 0; j--) %7B%0A        if( i * j %3C maxArea ) %7B%0A          break;%0A        %7D%0A        if(map.containsKey(j)) %7B%0A          Range range = map.get(j);%0A          if(min == 0 && max == 0 && flag) %7B%0A            min = range.min;%0A            max = range.max;%0A            flag = false;%0A    
+t.length - 1;%0A    while (left %3C right) %7B%0A      int lowerHeight;%0A      int length = right - left;%0A      if (height%5Bleft%5D %3C height%5Bright%5D) %7B%0A        lowerHeight = height%5Bleft%5D;%0A        left++;%0A
     
@@ -836,227 +836,90 @@
     
-    
-if(range.min %3C min) %7B%0A              min = range.min;%0A            %7D%0A            if(range.max %3E max) %7B%0A              max = range.max;%0A            %7D%0A          %7D%0A%0A          int length = max - min;%0A          int area = j
+lowerHeight = height%5Bright%5D;%0A        right--;%0A      %7D%0A      int area = lowerHeight
  * l
@@ -935,29 +935,25 @@
     
-    if(a
+maxA
 rea 
-%3E
+=
  maxArea
 ) %7B%0A
@@ -952,69 +952,33 @@
 Area
-) %7B%0A            maxArea = area;%0A          %7D%0A        %7D%0A      %7D
+ %3C area ? area : maxArea;
 %0A   
@@ -1049,26 +1049,24 @@
 args) %7B%0A    
-//
 System.out.p
@@ -1064,32 +1064,37 @@
 tem.out.println(
+4 == 
 maxArea(new int%5B
@@ -1097,22 +1097,26 @@
 int%5B%5D%7B1,
+ 
 2,
+ 
 3,
+ 
 4,
+ 
 1%7D));%0A  
@@ -1128,32 +1128,37 @@
 tem.out.println(
+1 == 
 maxArea(new int%5B
@@ -1161,16 +1161,17 @@
 int%5B%5D%7B2,
+ 
 1%7D));%0A  
@@ -1176,10 +1176,8 @@
     
-//
 Syst
@@ -1191,16 +1191,21 @@
 println(
+9 == 
 maxArea(
@@ -1220,16 +1220,21 @@
 %5D%7B1,
+ 
 2,
+ 
 3,
+ 
 4,
+ 
 1,
+ 
 5%7D))

solve no.11 and beats 95.41%