LeetCode 011 Container With Most Water *
来源:互联网 发布:淘宝加盟的骗局揭秘 编辑:程序博客网 时间:2024/04/30 21:28
题目:在平面直角坐标系上,给出(i,0)-(i,a_i)一系列线段,问哪两个线段和x轴组合起来产生的容器能装最多的水。
看到题目首先要问,在这个平面中的重力是什么方向的?能否倾斜容器?因为抛开重力方向谈装水都是耍流氓。题目中告诉我们,不可以倾斜容器,重力向下。那么根据木桶原理,能装水的区域取决于短的那根线段,即
area=min(height[i], height[j]) * (j - i) 假设j>i
最暴力的想法就是枚举i和j了,时间复杂度O(n^2)。这样显然会超时,所以要想更快的办法。我们可以用的是O(n)的扫描法。令i从0开始,j从n-1开始,i和j往中间移动。那么每次移动i还是j呢?我们移动height[i]和height[j]最小的那个,因为j-i减小了,要使area最大,保留长的线段更有潜力。
但是我没有想到数学上严谨的证明方法。为什么保留长的线段就是对的?说长的有潜力肯定不是很严谨。先打个*,以后注意。
0 0
- leetcode-011:Container With Most Water
- LeetCode 011 Container With Most Water
- LeetCode 011 Container With Most Water
- [LeetCode]-011-Container With Most Water
- leetcode 011 Container With Most Water
- LeetCode-011 Container With Most Water
- LeetCode 011 Container With Most Water
- 【LeetCode OJ 011】Container With Most Water
- [LeetCode]011-Container With Most Water
- LeetCode 011 Container With Most Water
- LeetCode 011 Container With Most Water *
- LeetCode 011: Container With Most Water
- leetcode--011 Container With Most Water
- leetcode-011-Container With Most Water
- 【LeetCode】011 Container With Most Water
- LeetCode-011 Container With Most Water
- LeetCode: Container With Most Water
- [LeetCode]Container With Most Water
- Unity3d+moba+UGUI摇杆
- R3全盘毁坏数据
- Android Service 回顾
- Linux环境搭建中文LaTeX排版系统
- mount img
- LeetCode 011 Container With Most Water *
- C++上机报告实验一
- LeetCode 325. Maximum Size Subarray Sum Equals k
- LeetCode 012 Integer to Roman
- LeetCode 270: Closest Binary Search Tree Value
- 1.安卓结构分析
- LeetCode 013 Roman to Integer
- LeetCode 014 Longest Common Prefix
- 欢迎使用CSDN-markdown编辑器