Problem:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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的更多相关文章

  1. LeetCode(11) Container With Most Water

    题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...

  2. LeetCode(11)Container With Most Water

    题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...

  3. LeetCode第[11]题(Java):Container With Most Water 标签:Array

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  4. LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  5. LeetCode(11)题解: Container With Most Water

    https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...

  6. LeetCode 笔记系列二 Container With Most Water

    题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  7. 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. leetcode第11题:盛水最多的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  9. leetcode个人题解——#5 Container with most water

    class Solution { public: string longestPalindrome(string s) { int length = s.length(); ) return s; ; ...

随机推荐

  1. C++ Primer 学习笔记_41_STL实践与分析(15)--先来看看算法【下一个】

    STL实践与分析 --初窥算法[下] 一.写容器元素的算法 一些算法写入元素值.在使用这些算法写元素时一定要当心.必须.写入输入序列的元素 写入到输入序列的算法本质上是安全的--仅仅会写入与指定输入范 ...

  2. 不同版本的SQL Server之间数据导出导入的方法及性能比较

    原文:不同版本的SQL Server之间数据导出导入的方法及性能比较 工作中有段时间常常涉及到不同版本的数据库间导出导入数据的问题,索性整理一下,并简单比较下性能,有所遗漏的方法也欢迎讨论.补充. 0 ...

  3. Code digest

    private void travelDir(String filepath) { String threadName = Thread.currentThread().toString(); log ...

  4. C语言API编写窗口界面和button

            近期有个同学的程序须要用对话框的方式实现,但前面都是通过黑框形式完毕的,老师突然让添加一个界面,本来准备採用MFC完毕的,但后来一想,该程序核心东西是体如今它的算法上,控制台的程序并不 ...

  5. 辛星整理3linux笔记,免费下载点,我希望对你有所帮助

    忙乱,这是我第一次看李指出老师的视频时,,这本书是关于116页面,在csdn下载对:点我下载 ,假设左边的地址崩溃了,也能够在浏览器中输入例如以下地址然后下载:http://download.csdn ...

  6. [SignalR]一个简单的聊天室

    原文:[SignalR]一个简单的聊天室 1.说明 开发环境:Microsoft Visual Studio 2010 以及需要安装NuGet. 2.添加SignalR所需要的类库以及脚本文件: 3. ...

  7. DNSserver内置笔记本

    DNS于linuxserver该服务名是named,和named服务相关的软件bind. 周围环境:     系统版本号:VBOX虚拟机centos6.0. 本机内网IP 192.168.2.198. ...

  8. Is it always safe to call getClass() within the subclass constructor?(转)

    14down votefavorite   An article on classloading states that the method getClass() should not be cal ...

  9. mysql三学习sql声明学习

    SQL 是一门 ANSI 的标准计算机语言,用来訪问和操作数据库系统.SQL 语句用于取回和更新数据库中的数据.SQL 可与数据库程序协同工作,比方MySQL. MS Access.DB2.Infor ...

  10. iOS学习 plist读取和写入文件

    干iOS开发时间.后经常用来plist文件,  那plist什么文件是它? 它的全称是:Property List.属性列表文件.它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plis ...