11. Container With Most Water
2018-02-23 18:17
288 查看
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 两边设一个指针,然后计算area,如果height[i] <= height[j],那么i++,因为在这里height[i]是瓶颈,j往里移只会减少面积,不会再增加area。 这是一个贪心的策略,每次取两边围栏最矮的一个推进,希望获取更多的水。 class Solution { public: int maxArea(vector<int>& height) { int res = 0; int i = 0,j=height.size()-1; while(i<j){ res = max(res, min(height[i],height[j])*(j-i)); height[i] > height[j]? j--:i++; } return res; } };
相关文章推荐
- 11. Container With Most Water
- 11. Container With Most Water
- leetcode:11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- Leetcode 11 Container With Most Water
- Leetcode-11:Container With Most Water
- leetcode 11 Container With Most Water
- leetcode习题解答:11. Container With Most Water
- LeetCode 11 Container With Most Water (C,C++,Java,Python)
- LeetCode - 11 - Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- [leedcode 11] Container With Most Water
- 【leetcode】Array——Container With Most Water(11)
- [LeetCode]11 Container With Most Water
- 11.Container With Most Water
- Container with most water_Leetcode_#11
- 11、Container With Most Water