原题链接在这里:https://leetcode.com/problems/container-with-most-water/
左右加班依次向中间移动,维护一个最大面积,每次移动小的那个夹板,因为储水量是由短的夹板决定的,移动大的夹板不会使面积更大。
Time O(n), Space O(1).
AC Java:
1 public class Solution { 2 public int maxArea(int[] height) { 3 if(height == null || height. length == 0){ 4 return 0; 5 } 6 7 int res = 0; 8 int low = 0; 9 int high = height.length-1; 10 while(low < high){ 11 int area = (high - low)* Math.min(height[low], height[high]); 12 res = Math.max(res, area); 13 if(height[low] < height[high]){ 14 low++; 15 }else{ 16 high--; 17 } 18 } 19 return res; 20 } 21 }
LeetCode Container With Most Water
原文:http://www.cnblogs.com/Dylan-Java-NYC/p/4839890.html