#11 Container With Most Water
2015-06-21 18:24
579 查看
原题链接:https://leetcode.com/problems/container-with-most-water/
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.
Note: You may not slant the container.
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.
Note: You may not slant the container.
int Min(int a, int b) { return a < b ? a : b; } //left,right分别初始化为第一个板和最后一个板,他们依次向中间移动,宽度变小,只有高度变大才有可能使总容量变大;而高度受限于较短板,所以只有移动较短的板才有可能使总容量变大 int maxArea(int* height, int heightSize) { int left = 0, right = heightSize - 1; int max = 0; while(left < right) { if(max < (right - left) * Min(height[left], height[right])) max = (right - left) * Min(height[left], height[right]); if(height[left] < height[right]) ++left; else --right; } return max; }
相关文章推荐
- DB2检查死锁和解锁的命令(AIX系统)
- 如何判断用paint画出的图形的坐标是在哪里?
- YT04-贪心课后练习-1006—PAINTER(6.14日-烟台大学ACM预备队解题报告)
- YT04-贪心课后练习-1002—Repair the Wall-(6.14日-烟台大学ACM预备队解题报告)
- How do you explain Artifical Neural Network to a college student ?
- 64位win7启动VirtualBox出错Error e_fail 0x80004005
- RAID磁盘阵列原理和实现
- 可用性 Availability 和可靠性 Reliability
- Training:卡特兰数(Catalan number)
- 真正的人工智能离我们有多远
- Training:Hash及应用
- Training:背包专题
- POJ 1218 THE DRUNK JAILER(数论)
- 谷歌上不去,长期的解决方案。在稳定高速Google和Gmail
- Contains Duplicate III
- leetcode-217-Contains Duplicate
- 小白的柳州麻将黑科技.Part1 = 你不知道的门清
- [POJ 1804] Brainman · 逆序对
- 【Leetcode】Contains Duplicate 1 and 2
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II