# LeetCode 笔记系列二 Container With Most Water

 1 //O(n^2)
2      public static int maxArea(int[] height) {
3          // Start typing your Java solution below
4          // DO NOT write main() function
5          int maxArea = 0;
6          for(int i = 1; i < height.length; i++){
7              if(height[i] == 0)continue;
8              for(int j = 0; j < i; j++) {
9                  int area = area(height,i,j);
10                  if(area > maxArea) {
11                      maxArea = area;
12                  }
13              }
14          }
15          return maxArea;
16      }
O(n^2)穷举

 1 public static int maxArea(int[] height) {
2          // Start typing your Java solution below
3          // DO NOT write main() function
4          int maxArea = 0;
5          for(int i = 1; i < height.length; i++){
6              if(height[i] == 0)continue;
7              int maxPossibleIdx = i - maxArea/height[i];
8              for(int j = 0; j < i && j <= maxPossibleIdx; j++) {
9                  int area = area(height,i,j);
10                  if(area > maxArea) {
11                      maxArea = area;
12                  }
13              }
14          }
15          return maxArea;
16      }
O(n^2)预判断

 1 public static int maxArea(int[] height){
2         int maxArea = 0;
3         int i = 0;
4         int j = height.length - 1;
5         if(j <=0)return 0;
6         while(i < j) {
7             int area = area(height, i, j);
8             if(height[i] < height[j]){
9                 i++;
10
11             }else {
12                 j--;
13             }
14             if(area > maxArea) maxArea = area;
15         }
16         return maxArea;
17     }
O(n)

posted on 2013-07-03 18:10  lichen782  阅读(5382)  评论(1编辑  收藏  举报