数组 Container With Most Water

来源:互联网 发布:中国进口数据分析 编辑:程序博客网 时间:2024/05/22 11:59

思想:

start初始指向0,end指向size-1;

若height[start]短,start++;

若height[end]短,end--;

即每次刷新最短的隔板。


class Solution {public:    int maxArea(vector<int>& height) {        int start = 0;        int end = height.size() - 1;        int max = INT_MIN;        while(start < end) {            int area = (end-start) * min(height[start], height[end]);            max = area > max ? area : max;            if(height[start] >= height[end]) {                end--;            }else {                start++;            }        }        return max;    }};


0 0
原创粉丝点击