一.题目链接: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. ps使用图层合并切图

    1.新建图层 2.合并要切的图的图层和新建的空白图层 3用正方形工具圈出来要切得图 4.ctrl+c复制,ctrl+n新建,ctrl+v复制过去 5,切图

  2. xdoj1321----简单搜索

    1321: 营救公主 时间限制: 1 Sec  内存限制: 128 MB提交: 156  解决: 37[提交][状态][讨论版] 题目描述 DSKer今天又做梦了,他的睡眠质量一直很差.他梦见他化身骑 ...

  3. 20155208徐子涵 2016-2017-2 《Java程序设计》第7周学习总结

    20155208徐子涵 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 时间与日期 13.1 认识时间与日期 就目前来说,即使标注为GMT(无论是文 ...

  4. PWA需要的技术

    1 Manifest                      https://developer.mozilla.org/zh-CN/docs/Web/Manifest 2 Service Work ...

  5. oracle 数据库中(创建、解锁、授权、删除)用户

    上文我们已经建立了名为orcl66的数据库. 想要在数据库中创建.修改用户需要我们以管理员权限登录到数据库中. 首先我们通过sqlplus命令登录连接数据库. 输入sqlplus命令--用户名: sy ...

  6. 【shell编程】之基础知识-文件包含

    和其他语言一样,Shell 也可以包含外部脚本.这样可以很方便的封装一些公用的代码作为一个独立的文件. Shell 文件包含的语法格式如下: . filename # 注意点号(.)和文件名中间有一空 ...

  7. node学习笔记之io.sockets

    socket.get和socket.set函数已经失效,代码修改如下所示: 服务器端: var httpd = require('http').createServer(handler); var i ...

  8. GoLand tool tips

    goland format code (on save ) preference -> tool -> file watcher  then ,select go fmt 2, highl ...

  9. linux日志管理

    //有关当前登录用户的信息记录在文件utmp中 //登录进入和退出纪录在文件wtmp中 [root@bogon python]# who //who命令查询utmp文件并报告当前登录的每个用户 /va ...

  10. Apache和Nginx负载均衡集群及测试分析

    一.应用场景介绍 本文主要是介绍Apache和Tomcat在Linux环境下的安装讲解以及AJP协议动静分离负载均衡的实现,以及与Nginx负载性能比较.联网安装较为简单,故此处只说脱机的Linux环 ...