leetcode 11. Container With Most Water
来源:互联网 发布:广东工业大学矩阵分析 编辑:程序博客网 时间:2024/06/05 16:18
相关问题
Discription
Given n non-negative integers
Note: You may not slant the container and n is at least 2.
思路
双指针:用两个指针分别表示容器的左边界 left 和右边界 right。
初始 left 指向最左,right 指向最右。
考虑如下两点事实:
- 容器的盛水量取决于短的边界高度
min(height[left], height[right]) 以及水平长度 right - left ; - 随着 left 向右移动或者 right 向左移动,水平长度必然减小;
故只有提高短边界的高度
时间复杂度:
空间复杂度:
代码
class Solution {public: int maxArea(vector<int>& height) { int left = 0, right = height.size() - 1; int max_area = min(height[left], height[right]) * (right - left); while (left < right) { int min_h = min(height[left], height[right]); if (height[left] == min_h) // 找到瓶颈 while (left < right && height[left] <= min_h) left++; else while (left < right && height[right] <= min_h) right--; if (left < right) { int area = min(height[left], height[right]) * (right - left); max_area = max(area, max_area); } else break; } return max_area; }};
阅读全文
0 0
- 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
- 【leetcode】11. Container With Most Water
- [LeetCode]11. Container With Most Water
- 11. Container With Most Water LeetCode
- leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- [LeetCode OJ]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
- LeetCode *** 11. Container With Most Water
- dubbo 源码学习笔记 (四) —— 配置模块
- leaked ServiceConnection
- Linux编译安装Python3.X版本并保留系统自带的Python2.X版本
- Tomcat原理
- Tomcat服务器修改默认编码格式
- leetcode 11. Container With Most Water
- Java获取文件后缀的最简单方式
- (M)DFS: 547. Friend Circles
- 函数:折半查找与数组的逆置
- 【深入PHP 面向对象】读书笔记(十六)
- js全局变量和局部变量
- Servlet 笔记 (五 Cookie)
- Poj 1065 Wooden Sticks
- 基于IDSS和Machine Learning的零售金融大数据分析(二)