LeetCode——11. Container With Most Water
一.题目链接: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的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 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 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解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 My Submissions Question 解题思路
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [leetcode]11. Container With Most Water存水最多的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
随机推荐
- 基于Hexo+Node.js+github+coding搭建个人博客——基础篇
附上个人教程:http://www.ookamiantd.top/2017/build-blog-hexo-base/ 搭建此博客的动机以及好处在此就不多谈了,之前已经表达过,详情请看Start My ...
- golang图片裁剪和缩略图生成
直接贴代码了 package main import ( "errors" "fmt" "image" "image/gif&qu ...
- Thread_run()方法
cas 1: package threadTest; public class ThreadTest { public static void main(String[] args) { Thread ...
- 2017.4.9 函数式编程FP
函数编程(简称FP)不只代指Haskell Scala等之类的语言,还表示一种编程范式,和面向对象的编程方式一样,是编程思维,软件思考方式,也称面向函数编程. 编程的本质是组合,组合的本质是范畴Cat ...
- windows server 2008 R2无法共享文件夹,无法启用网络发现。
问题描述:在局域网内两台window server 2008 R2服务器上设置共享文件夹,如要再网络中可以看到文件夹,需要开启“网络发现” 但是即使打开了“网络发现”,当重新打开“高级共享设置”的时候 ...
- itcast-spring-三大框架整合
三大框架架构(整合原理) struts整合到spring hibernate整合到spring 导包 eclipse需要导入 myeclipse不用 单独配置spring容器 单独配置stru ...
- Centos7修改默认启动级别(命令行,图形切换)
方法一: 终端输入以下命令 修改为命令行方式 systemctl set-default multi-user.target 修改为图形界面 systemctl set-default graphic ...
- getpwnam,getgrnam,getpwent,crypt等函数
[root@bogon code]# cat a.c #include<stdio.h> #include<pwd.h> int main() { struct passwd ...
- How To Use the AWK language to Manipulate Text in Linux
https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-l ...
- C# Monitor的Wait和Pulse方法使用详解
[转载]http://blog.csdn.net/qqsttt/article/details/24777553 Monitor的Wait和Pulse方法在线程的同步锁使用中是比较复杂的,理解稍微困难 ...