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; ; ...
随机推荐
- sql server 2008如何导入mdf,ldf文件
sql server 2008怎样导入mdf,ldf文件 网上找了非常多解决sql server导入其它电脑拷过来的mdf文件,多数是不全.遇到的解决方法不一样等问题,下边是找到的解决这个问题的最 ...
- C++ Primer笔记4_静态成员类_IO库
1.静态成员类 static成员变量与函数 static成员变量:必须在类外初始化.(const或引用类型变量必须在构造函数初始化列表里初始化) static成员函数: 不依赖于类.相当于类里的全局函 ...
- PHP上传文件(学习)
<?php if(isset($_FILES['upfile'])) { if (is_uploaded_file($_FILES['upfile']['tmp_name'])){ $upfil ...
- Linux内核分析(三)----初识linux内存管理子系统
原文:Linux内核分析(三)----初识linux内存管理子系统 Linux内核分析(三) 昨天我们对内核模块进行了简单的分析,今天为了让我们今后的分析没有太多障碍,我们今天先简单的分析一下linu ...
- 基于NSIS脚本开发的安装程序制作软件:易量安装
原文 基于NSIS脚本开发的安装程序制作软件:易量安装 前几天“萝卜”给我推荐了一款安装程序制作工具——易量安装. 易量安装是一款安装程序制作软件,基于著名的NSIS(Nullsoft Scripta ...
- Struts1和Struts2对照
最近学习Struts1和Struts2.好多人.提到非常多的信息. Struts2不从Struts1进化. Struts2的前身WebWork. 呢,看了一些资料,下边就来比較比較. 一.架构分析 S ...
- sqlplus登录问题
问题1.sqlplus login -- SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory 在/e ...
- 用fcntl()设置堵塞函数的堵塞性质
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types. ...
- 【百度地图API】如何用圆形搜索获取中心点周围100米内全部关键点?如天安门附近所有的餐厅、加油站、宾馆、大厦等
原文:[百度地图API]如何用圆形搜索获取中心点周围100米内全部关键点?如天安门附近所有的餐厅.加油站.宾馆.大厦等 摘要: 在LBS上有这样一个常用的功能,查找附近所有的关键点(POI点,比如标志 ...
- Linux常用命令2--用户问题、文件的打包压缩
Linux常用命令 如何进行用户和群组的创建和更改 [1]groupadd:用于创建新的群组. 语法:groupadd [-option] 用户名:其常用参数有:-g groupadd -g 555 ...