码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Container With Most Water

时间:2015-09-26 07:00:41      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

原题链接在这里: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!