Leetcode 11-Container With Most Water
2018-01-05 18:31
399 查看
Leetcode
11-Container With Most Water
暴力不可取首先,自然想到最左边和最右边的wall有着最大的宽度。所以如果不用这个最大的宽度,我们就需要两堵墙有着更大的高度。
设最左边的wall有高度h1 最右边的wall高度h2, 之间宽度为w
则s = w*min(h1,h2)
对于h = min(h1,h2)的wall来说
设再选择any wall的高度为h’,都有w’ < w, min(h, h’) <= h
=> s’ < s
所以我们可以丢弃这一堵墙。
int l = 0; int r = height.size() - 1; int res = min(height[l], height[r]) * (r - l); while (l < r) { height[l] > height[r] ? r--:l++; res = max(res, min(height[l], height[r]) * (r - l)); } return res;
相关文章推荐
- LeetCode 11 Container With Most Water
- Leetcode-11:Container With Most Water
- [Leetcode 11, Medium] 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 [Python]
- 011_LeetCode_11 Container With Most Water 题解
- LeetCode 11: Container With Most Water
- LeetCode11-Container With Most Water
- LeetCode 11 Container With Most Water (C,C++,Java,Python)
- [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
- [leetcode-11]container with most water(C)
- LeetCode11: Container With Most Water
- leetcode:11. Container With Most Water
- LeetCode 11 - Container With Most Water