Container With Most Water

brute force O(n2)

从头尾两头扫面 O(n)

 1 public class Solution {
 2     public int maxArea(int[] height) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         int start = 0;
 6         int end = height.length - 1;
 7         int max = 0;
 8         if(height == null)
 9             return 0;
10         if(height.length < 2)
11             return 0;
12         while(start < end)
13         {
14             int tmp = (end - start) * Math.min(height[start], height[end]);
15             if(tmp > max)
16                 max = tmp;
17             if(height[start] >= height[end])
18             {
19                 int oldend = end;
20                 do{end--;} while(start < end&&height[end] < height[oldend]);
21             }
22             else
23             {
24                 int oldstart = start;
25                 do{start++;}while(start < end&&height[start] < height[oldstart]);
26                 
27             }
28         }
29         return max;
30         
31     }
32 }
原文地址:https://www.cnblogs.com/jasonC/p/3407880.html