leetcode第11题--Container With Most Water
Problem:
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.
题目的意思是,在(x,y)坐标中,每个点做与x轴垂直的直线后,求哪两根直线和x轴所能装的水最多,不能倾斜的意思就是不是指梯形的面积,而是短板效应的矩形面积。先暴力试了下,练了下手感。不出所料N方的超时。
class Solution {
public:
int maxArea(vector<int> &height)
{
int area = , temparea;
int *temp = new int[height.size()];
for (int i = ; i < height.size(); i++)
{
int maxN = ;
for (int j = ; j < height.size(); j++)
{
if (i != j)
{
temparea = (height[i]<height[j]?height[i]:height[j]) * abs(j - i);
if (temparea > maxN)
maxN = temparea;
}
}
temp[i] = maxN;
}
for (int i = ; i < height.size(); i++)
{
if (temp[i] > area)
area = temp[i];
}
delete[] temp;
return area;
}
};
后来还想,能不能排序之后再判断,发现sort又是不稳定的所以就放弃了。后来发现可以从两边往里收缩的办法解决。两边往里的还有第一题Two Sum也是这样做的。
为什么用两边往里呢,因为我们我们要的面积是两条直线的距离*两条直线短的那条的值,所以,我们先定一个,距离最大就是头和尾了,如果比这个距离小的,又还想比我现在大的话,那就只有高增加才有可能,这个时候就是把短的那条对应的位置往前看一位,看看可不可能有比短的长,且乘出来的面积是比之前大的,如果大,那就记录下来。为什么要用短的那边往里看呢,因为如果长的往里的话,就是下去一根再长也是根据短板效应看短的。根据这个思路自己整理了下代码如下:
class Solution {
public:
int maxArea(vector<int> &height)
{
int left = , right = height.size() - ;
int maxA = ;
while(left < right)
{
if (height[left] < height[right])
{
int tmp = (right - left) * height[left];
left++;
if (tmp > maxA)
maxA = tmp;
}
else
{
int tmp = (right - left) * height[right];
if (tmp > maxA)
maxA = tmp;
right--;
}
}
return maxA;
}
};
这样就Accept了
2015/03/29:
class Solution {
public:
int maxArea(vector<int> &height) {
int l = , r = height.size()-, water = ;
while(l < r){
water = max(water, (r - l) * (height[l] > height[r] ? height[r--] : height[l++]));
}
return water;
}
};
python:
class Solution:
# @return an integer
def maxArea(self, height):
water, l, r = 0, 0, len(height)-1
while l < r:
water = max(water, (r - l)*min(height[l], height[r]))
if height[l] < height[r]:
l += 1
else:
r -= 1
return water
leetcode第11题--Container With Most Water的更多相关文章
- LeetCode(11) Container With Most Water
题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...
- LeetCode(11)Container With Most Water
题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...
- LeetCode第[11]题(Java):Container With Most Water 标签:Array
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- LeetCode(11)题解: Container With Most Water
https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...
- LeetCode 笔记系列二 Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetcode第11题:盛水最多的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- leetcode个人题解——#5 Container with most water
class Solution { public: string longestPalindrome(string s) { int length = s.length(); ) return s; ; ...
随机推荐
- 左右 Java 于 finally 深度分析语句块
首先,让我们来问你一个问题:finally 声明块将运行? 很多人认为 finally 语句块是一定要运行.其中还包括了一些非常有经验的 Java 程序猿.不幸的是,没有像很多像人们想象,对于这个问题 ...
- Robot Framework 使用1-环境配置及简单网站兼容性测试(转)
0.Robot Framework 简介 Robot Framework 是一个通用的自动化测试框架,主要用于“验收测试”和“验收测试驱动开发(ATDD)” (会其它文章中会详细介绍ATDD).它使用 ...
- ocp认证考试指南第一章
数据库存储结构: 物理数据库存储结构: 必须的三类文件: 控制文件(controlFile):multiplexing the controfile 多路复用控制文件,指向其它关键文件,存储序列号和时 ...
- 【代码优化】当许多构造函数的参数,请考虑使用builder模式
静态工厂和构造具有共同的局限性:我们不能扩展到大量的非常好的可选参数. 1.对于多个可选參数的构造器.我们都习惯採用重叠构造器模式.比方一个參数的构造器调用2个參数的构造器. 2个參数的构造器 ...
- ARP协议的基础知识
关于ARP协议的基础知识 1.ARP的工作原理 本来我不想在此重复那些遍地都是的关于ARP的基本常识,但是为了保持文章的完整性以及照顾初学者,我就再啰嗦一些文字吧,资深读者可以直接跳过此节 ...
- eclipse 对齐行号在括号中显示和字体调整
笔者 : 本笃庆军 一.括号对齐:指和C/C++里面一样.上下括号对齐~~~ 第一步:Project->preferences->Java->Code Style->Forma ...
- Mule ESB-Content-Based Routing Tutorial(2)
承接 Mule ESB-Content-Based Routing Tutorial(1) 五.执行应用程序 完毕创建,配置.并保存你的新的应用程序,您就能够在嵌入Mule的server上执行(包含 ...
- 你可以不知道原因,但是,我们不能停止努力。httplook抓取路由配置界面信息
因为实验室的项目要求,需要动态控制路由的配置信息,我们知道.路由选择需要购买的访问后,:http://192.168.1.1 路由配置页面.配置,不须要人手工操作.甚至定时任务配置的时候,就须要进一步 ...
- 【iOS开展-94】xcode6如何使用GIT以及如何添加太老项目GIT特征?
(1)对于一个新项目:如何使用GIT?在新项目的过程,例如,您可以选择下面的复选框. (2)针对老项目,加入GIT功能. --在终端.cd到项目文件夹 --然后输入git init,初始化一个.git ...
- ASP.NET MVC2.0 自定义filters
今天大家共同学习下ASP.NET MVC2.0中自定义filters,这一节主要学习下ActionFilterAttribute, ActionFilterAttribute继承IActionFilt ...