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; ; ...
随机推荐
- zigbee、profile、cluster、 endpoint、
1.引用ZigBee联盟的说法 Profile: a collection of device descriptions, which together form a cooperative appl ...
- 24L01/SI24R1调试笔记
1.SPI MSB优先,8Bit寄存器地址与内容: 2.寄存器结构与之前使用的LT8900不同,分为R.W寄存器与特殊功能寄存器: 3.特别注意:在TX.RX.RT中断或者轮询后置1,必须写1清零与清 ...
- 高通公司 MSM8K GPT异常原因分析无法开机的问题
问题分析过程如下面: 一. MSM8916台gpt概率问题:采用QPST emmc software download下载软件工具后,无法开机.例如下面的附图: log分析是userdata分区未成功 ...
- CIC and Fir 滤波器的级联
在FDATool中 CIC 和 Fir 级联滤波器的设计 1 设计CIC滤波器的幅频特性曲线如下 2.设计FIR 滤波器的幅频特性曲线如下 3.总的特性曲线如下 4.把通带部分放大后的图,比较平坦
- java 科学计算库
stackoverflow 上的讨论, 其中不乏lib的作者... http://stackoverflow.com/questions/529457/performance-of-java-matr ...
- FloatyFish下载量
老师之前没有统一好一个平台,为了公平起见,我们选择了知名度比较高的CSDN,上次课上给老师说的下载量已成为过去,我们目前的下载量是: 这里还有我们最真实的用户体验,来自CSDN的用户,而非我们的朋友: ...
- shell 中用管道模拟多线程
shell 中用管道模拟多线程 这里以两个例子来对比多线程和单进程 单线程的例子 # config.txt在这个例子和多线程的例子中都会用到 [root@ns_10.2.1.242 test]$ ca ...
- Tiptop二二次开发系列
易拓GP 4gl二二次开发系列.使用文档工具的深度开发,包装分享. 包含文件: Genero BDL HB 2.0 .pdf Genero Studio 4GL官方开发手冊.pdf 下载地址:http ...
- 如何使用Linq或EF来对数据去重——Distinct方法详解
刚开始接触LINQ时使用distinct去重时和大家一样遇到了一些麻烦,很感谢 http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.htm ...
- css3布局相关(持续更新)
1三栏布局,两边定宽,中间自适应 2让文字位于div元素的正中央 3不管浏览器窗口如何变化,让一张图片始终显示在浏览器正中央.