11. Container With Most Water
来源:互联网 发布:知乎雪花釉餐具安全吗 编辑:程序博客网 时间:2024/06/07 11:25
Question: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 and n is at least 2.
Answer:
public class Solution { public int maxArea(int[] height) { int maxarea = 0, l = 0, r = height.length - 1; while (l < r) { maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l)); if (height[l] < height[r]) l++; else r--; } return maxarea; }}
问题的关键在两边加减以后能不能保证被忽略的容量会不会更大
Here is a simple proof for the solution.Use v[low, high] indicates the volume of container with low and high. suppose height[low] < height[high], then we move low to low+1, that means we ignored v[low, high-1],v[low, high-2],etc, if this is safe, then the algorithm is right, and it's obvious that v[low, high-1],high[low, high-2]...... can't be larger than v[low, high] since its width can't be larger than high-low, and its height is limited by height[low].
阅读全文
0 0
- 11.Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11.Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 617. Merge Two Binary Trees
- Idea 2017.2.4 破解 可用方法,请看到最后一行
- Android App优化之性能分析工具
- Java编程思想-一切皆对象
- HTTP个人总结(五)
- 11. Container With Most Water
- Java线程池的使用
- Android基于AOP的非侵入式监控之——AspectJ实战
- MySQL Replication之主从切换
- Android Studio下的应用性能优化总结-内存优化
- Anaconda 安装与 使用
- activiti用户
- 如何提高学习java开发的效率和技术(自学者参考)
- 基于java web的档案管理系统的设计与实现