JeromeHuang

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

 

其实就是求两根杆子之间能容纳的水量,不用考虑中间的杆子。

26ms

class Solution {
public:
    int maxArea(vector<int>& height) {
        if (height.empty()){
            return 0;
        }
        int l = 0, h = height.size() - 1;
        int max = (h - l)*(height[l] > height[h] ? height[h] : height[l]);
        while (l < h){
            if (height[l] > height[h]){
                h--;
            }
            else{
                l++;
            }
            int temp = (h - l)*(height[l] > height[h] ? height[h] : height[l]);
            if (temp > max){
                max = temp;
            }
        }
        return max;
    }
};

 

posted on 2015-04-23 22:47  JeromeHuang  阅读(121)  评论(0编辑  收藏  举报