Container With Most Water

来源:互联网 发布:最精准的足彩数据分析 编辑:程序博客网 时间:2024/06/06 13:55

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.

Note: You may not slant the container and n is at least 2.



class Solution {

public:
    int maxArea(vector<int>& height) {
        vector<int> v = height;
        int begin = 0;
        int end = v.size() - 1;
        int max = 0;
        int temp = 0;
        max = (end - begin) * lower(v[begin],v[end]);
        while(begin < end){
        temp = (end - begin) * lower(v[begin],v[end]); 
        max = max > temp?max:temp;
        if(v[begin] > v[end]) end--;
        else begin++;
        }
        return max;
    }
    int lower(int a,int b){
    return a > b ? b : a;
    }
};
原创粉丝点击