LeetCoder 11. Container With Most Water
来源:互联网 发布:中国专利检索数据库 编辑:程序博客网 时间:2024/06/06 18:00
题意
- 例如:
3 条线为a[0]=3,a[1]=4,a[2]=5 ,那么最大面积为3∗(2−0)=6
思路
解法一:
暴力,枚举两条边,然后计算取最大面积,时间复杂度
解法二:
贪心,定义左右两个指针
l 指针,如果想要面积大于当前的遍历点,我们应该尽量寻找让右边线作为面积的高的情况,那么只能寻找一个大于右边线长度的左边线,但是,中间有可能会有线和右边线进行组合而大于我们记录的面积,所以说,在遍历过程中进行检查.r 指针,同理,要尽量寻找让左边线作为面积高的情况,那么只能寻找一个大于左边线长度的右边线,同样的,中间可能会有线和左边线进行组合而大于我们记录的面积,所以说,在遍历中同样要进行检查.
时间复杂度
结果
Your runtime beats 48.53 % of cpp submissions
代码
class Solution {public: int maxArea(vector<int>& height) { int l = 0; int r = height.size() - 1; int ans = 0; while(l < r){ int tempL = height[l]; int tempR = height[r]; while(height[l] <= tempR && l < r) { ans = max(ans,(r - l) * min(height[l], height[r])); l++; } while(height[r] <= tempL && r >= l){ ans = max(ans,(r - l) * min(height[l], height[r])); r--; } } return ans; }};
0 0
- LeetCoder 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
- SSM整合之applicationContext.XML配置文件详解
- 从单层感知器到深度学习以及深度学习必知的框架
- 动态规划解决01背包问题(java实现)
- Java入门学习-学会Scanner的用法。做出一个计算器加法功能。
- Codeforces Round #401 (Div. 2)C. Alyona and Spreadsheet
- LeetCoder 11. Container With Most Water
- Java进阶——详解反射机制及应用和曾经踩过的一个坑
- web.xml文件详解
- 实例探索Java模式之路——序言
- 设计模式——设计模式中英文对照
- 自己学习网页制作,写的私人博客
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- Jetson TK1 刷机步骤小记
- webpack+es6+angular1.x项目构建