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; ; ...
随机推荐
- window.open的小技巧分享(转)
今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明. 聊到window.open,不得不说明一下他的使用方法,主要有两种形式: window. ...
- 使用Inno Setup 打包jdk、mysql、tomcat、webapp等为一个exe安装包(转)
之前一直都没涉及到打包安装方面的东西,都是另一个同事负责的,使用的工具(installshield)也比较高大上一点,可是后来他离职以后接受的同事也只能是在这个基础上做个简单的配置,然后打包,可是现在 ...
- 初探boost之progress_display库学习笔记
progress_display 用途 progress_display能够在控制台上显示程序的运行进度,假设程序运行非常耗费时间,那么它能提供一个友好的用户界 面,不至于让用户在等待中失去耐心,甚至 ...
- 使用exchange普通表模式被切换到分区表
随着数据库的不断增长的数据量.有些表需要转换的普通堆表分区表模式. 有几种不同的方式来执行此操作,如出口数据表,区表再导入数据到分区表:使用EXCHANGE PARTITION方式来转换为分区表以及使 ...
- redis和redis php扩展安装(转)
redis是一个内存数据库,比memcache支持更丰富的value类型,新浪微博就使用redis来做缓存. redis的源码安装 wget http://download.redis.io/redi ...
- vs2013 ADO联系SQL server2012数据库
平时,给定ADO例如使用以下过程数据源中的数据的数据库应用程序 (1) 创建一个Connection 物.定义的连接字符串信息.它包含了数据源名称.用户ID.密码.连接超时 . 默认数据库的位置和光标 ...
- hdu 4407 Sum 容斥+当前离线
乞讨X-Y之间p素数,,典型的纳入和排除问题,列的求和运算总和的数,注意,第一项是最后一个项目数. 如果不改变到第一记录的答案,脱机处理,能保存查询,候,遇到一个操作1,就遍历前面的操作.把改动加上去 ...
- (大数据工程师学习路径)第四步 SQL基础课程----select详解
准备 在正式开始本内容之前,需要先从github下载相关代码,搭建好一个名为mysql_shiyan的数据库(有三张表:department,employee,project),并向其中插入数据. 具 ...
- JavaScript中null和undefined的总结
先说null,它表示一个特殊值,常用来描述“空值”.对null执行typeof运算,结果返回字符串“object”,也就是说,可以将null认为是一个特殊的对象值,含义是“非对象”(感觉怪怪的).实际 ...
- Android使用surface直接显示yuv数据(三)
在本文中,Java创建UI和关节JNI经营层surface直接显示yuv数据(yv12).发展环境Android 4.4,驰A23平台. package com.example.myyuvviewer ...