Leetcode Container With Most Water

来源:互联网 发布:网店数据化分析的内容 编辑:程序博客网 时间:2024/04/30 13:11

Container With Most Water

 Total Accepted: 4630 Total Submissions: 15434My Submissions

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.


知道原理就是很简单的程序。两边搜索,短板往里走。因为往里走,代表宽度减小,那么宽度小的时候,只有遇上更高的高度才能组成更加大的container。

int maxArea(vector<int> &height)     {    int area = 0;    int i = 0, j = height.size()-1;    while (i<j)    {    area = max(area, (j-i)*min(height[i], height[j]));    (height[i] > height[j])? j--:i++;    }    return area;    }


1 0
原创粉丝点击