一.题目链接:https://leetcode.com/problems/container-with-most-water/

二.题目大意:

  给定n个非负整数a1,a2....an;其中每一个整数对应着一条垂直的线段,即(i,ai)到(i,0)这个线段。其中这n个线段中,任意两个线段与x轴就构成了一个容器;要你找出体积最大的容器出来,并返回它的体积值。

三.题解:

  这道题目最容易想到的就是暴力法,从n个线段中任取两个来组成容器进行判断。

方法1:

  暴力法,代码如下:

class Solution {
public:
int maxArea(vector<int>& height) {
int s = height.size();
int max_container = 0;
for(int i = 0 ; i < s; i++)
{
for(int j = i + 1; j < s; j++)
{
int temp = height[i] > height[j]?height[j]:height[i];
int temp_container = temp * (j - i);
if(temp_container > max_container)
max_container = temp_container;
}
}
return max_container;
}
};

该算法的时间复杂度为O(n2),空间复杂度为O(1),但是,最终结果会超时!

方法2:

  仔细想想整个过程,我们会发现:对于两个长度不同的线段构成的容器,容器的体积取决于较短的那个线段。所以,我们可以利用这么一种方法:定义两个指针(L指针和R指针),分别指向数组的头部和尾部,然后每次比较L和R指向的线段长度大小,如果L指针指向的线段长度小的话,L指针向右移动;如果R指针指向的线段长度小的话,R指针向左移动。然后每次计算容器的体积,选择最大的那个作为最终结果。(至于方法为什么可行,在后面解释)代码如下:

class Solution {
public:
int maxArea(vector<int>& height) {
int c_len = height.size();
int max_container = 0;
int l_index = 0, r_index = c_len - 1;
while(l_index < r_index)
{
int temp = (r_index - l_index) * min(height[l_index],height[r_index]);
max_container = max_container>temp?max_container:temp;
if(height[l_index] < height[r_index])
l_index++;
else
r_index--;
}
return max_container;
}
};

该算法的时间复杂度为O(n),空间复杂度为O(1)。下面详细解释一下这种方法:

1.首先,要了解到的一点是此处的容器相当于一个两边不相同的长方形,而不是一个圆柱,所以它的体积计算公式是:底 x 高。而不是π x R2 x h 。

2.由于容器的体积是由较小的那个线段决定,假设L指针指向元素A,R指针指向元素B,如果A<B的话;那么,对于B左边所有的边(A除外),它们与A构成的容器体积一定小于A与B构成的容器体积。

证:假设A与B构成的容器的底为L1、容器的高为H1,A与B左边其他线段构成的容器的底为Li、高为Hi,则Li < L1一定成立,H1≥Hi也是一定成立的(因为体积取决于较小的线段,如果Hi > H1,则Hi = H1;否则Hi不变)所以Li x Hi一定小于L1 x H1。

所以当A<B的话,L指针向左移动,而不是R指针向右移动。这样就相当于,A不用再与B左边其他的线段配对了(相比暴力而言省去了很多没用的比较),这样的话每次判断哪个线段小,就移动相应的指针。

3.但是,如果A==B的话该怎么办?

实际上,当A==B的话,这种情况是不需要特别考虑的。即此时L指针向右移动也行,R指针向左移动也可以。为什么呢?因为此时的A和B都可以看作是较小的那个线段,根据第1步,L和R谁移动都是合理的。并且无论是L指针向右移动还是R指针向左移动,它们都遍历了A、B之间所有的线段,所以这两种移动情况结果是一样的。

LeetCode——11. Container With Most Water的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  4. LeetCode 11. Container With Most Water (装最多水的容器)

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

  5. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  6. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  7. Java [leetcode 11] Container With Most Water

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

  8. C#解leetcode 11. Container With Most Water

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

  9. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

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

  10. [leetcode]11. Container With Most Water存水最多的容器

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

随机推荐

  1. ZOJ5833 Tournament(递归打表)

    题目链接:传送门 假思路: 根据题意要求,只能按字典序最小的方法安排比赛. 所以第一场必定是1和2比,3和4比.... 选手:1 2 对手:2 1 根据要求如果1与2比过赛了,1再与其它的人(不妨设为 ...

  2. easyui表单校验

    痛苦使人清醒,痛苦使人警惕.生于忧患,死于安乐.付出总会有回报. 1.下面跟大家分享使用easyui时表单中的值如何校验. 1.1 首先,在你的jsp/html页面引入JQuery和easyui的js ...

  3. java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie

    https://blog.csdn.net/cml_blog/article/details/52135115 当项目中使用单点登录功能时,通常会使用cookie进行信息的保存,这样就可以在多个子域名 ...

  4. Sencha Touch app example -- oreilly app 分析

    from: 2013/8/30的笔记 使用development.js 读取 app.json 配置文件 app.json 配置了app.js文件 app.js lauch function ,首先用 ...

  5. LG5056 【模板】插头dp

    题意 题目背景 ural 1519 陈丹琦<基于连通性状态压缩的动态规划问题>中的例题 题目描述 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? 输 ...

  6. scanf() gets() fgets()使用注意事项

    1.scanf() 遇到'\n'停止从输入缓冲区中接收,接收完后‘\n’还存在于缓冲区中.当输入的字符数少于缓冲区大小时,字符后面有自动补上‘\0’,当输入字符大于缓冲区时,也直接拷贝到缓冲中,因此缓 ...

  7. 02c语言指针基础

    & 用来取一个变量的地址 * 用来取一个地址的值 例如: (1)&n 获取n的地址 int *p=&n; *p就等于p的值 (2) 假设 int n=10; *(&n) ...

  8. SWF 文件不能本地访问 只有仅限于文件系统的 SWF 文件

    http://blog.163.com/vituk93@126/blog/static/1709580342012512112757505/ SWF 文件不能被本地访问 不能访问本地 只有仅限于文件系 ...

  9. java_生态环境

    Which Java package do I need? Software Developers: JDK (Java SE Development Kit). For Java Developer ...

  10. Java Scanner学习记录

    1. Java.util.Scanner可以用来从键盘获取输入 Scanner.next()  只能读取字符,遇到任何的符合都不会输出 Scanner.nextLine()  会完全按照用户输入的st ...