leetcode-algorithms-11 Container With Most Water
leetcode-algorithms-11 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 and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
解法1
列出所有情况,找到最大值.
class Solution
{
public:
int maxArea(vector<int>& height)
{
int area = 0;
for (int i = 0; i < height.size(); ++i)
{
for (int j = i + 1; j < height.size(); ++j)
{
int low = height[i] > height[j] ? height[j] : height[i];
int temp_area = low * (j - i);
if (temp_area > area)
area = temp_area;
}
}
return area;
}
};
时间复杂度: O(n^2).
空间复杂度: O(1).
解法2
要算出最大的面积,不考虑高度的情况下,肯定是长度越长,面积越大.接着,长度减小,面积是由短的那个决定的,因此,要使得到面积更大,就只需要移动短的那个高度.所以可得到下面的算法.
class Solution {
public:
int maxArea(vector<int>& height) {
int area = 0;
int left = 0;
int right = height.size() - 1;
while(left < right)
{
int low = height[left] < height[right] ? height[left] : height[right];
int temp_area = low * (right - left);
if (temp_area > area)
area = temp_area;
if (height[left] < height[right])
++left;
else
--right;
}
return area;
}
};
时间复杂度: O(n).
空间复杂度: O(1).
leetcode-algorithms-11 Container With Most Water的更多相关文章
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】11. Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode problem 11 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode OJ 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
随机推荐
- 什么是mvc?
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型 (Model).视图(View)和控制器(Controller). ...
- oogle advertiser api开发概述——速率限制
速率限制 为了向遍布全球的 AdWords API 用户提供可靠的服务,我们使用令牌桶算法来衡量请求数并确定每秒查询数 (QPS) 速率.这样做的目的是阻止恶意的或不可控的软件大量入侵 AdWords ...
- 【转载】谈谈自己对REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解
转载自:https://blog.csdn.net/tantexian/article/details/48196453 SOA: 维基百科解释:SOA:面向服务的软件架构(Service Orien ...
- awk详解2
7.控制语句 if(condition) {statments} 单分支语句 if(condition) {statments} else {statements}组合语句 while(condito ...
- perl hash array 嵌套 push
$hash{"A"}=["pp"];想变成:$hash{"A"}=["p","q"];因为 $has ...
- Java问题解决:Java compiler level does not match the version of the installed Java project facet.
问题原因:Java编译器级别与Facted Project 中的Java 版本设定不匹配. 解决办法:将两者设置一致 1.查看Java compiler level : 选中项目右键propertie ...
- eclipse中.project文件和.classpath文件详解
一.概述.project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置..classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH. 二. ...
- eclipse maven maven-compiler-plugin 报错 完全解决
报错如下: Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:comp ...
- STL_string.vector中find到的iterator的序号
ZC:注意,printf("0x%08X\n",vtr.end()); 打印出来 应该就是 0x00000000,∵ 它就是 指向最后一个元素的后面,应该是理解成 无意义 ...
- [osg]节点遍历nodevisitor浅析
参考:https://www.cnblogs.com/hzhg/archive/2010/12/17/1908764.html OSG中节点的访问使用的是一种访问器模式.一个典型的访问器涉及抽象访问者 ...