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). ...
随机推荐
- tensorboard 可视化
#coding = utf8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mn ...
- Bagging-Adaboost-RF的粗糙理解
三种方法都是组合方法,组合方法是使用多个分类器进行投票[构造每个分类器的样本都是通过有放回抽样得到的] 1.Bagging(装袋):k次抽样,训练k次,得到k个模型(分类器),等权重投票 2.Adab ...
- linux入门001--帮助支持
linux入门001--帮助支持====1. 帮助手册提供命令的使用说明:man ls提供基础知识和参考信息,有时会有实例和交叉索引,但是基本没有教程式的文档.就是说,并不会有偏重告诉你那个重要,那个 ...
- 《DSP using MATLAB》Problem 5.12
1.从别的地方找的证明过程: 2.代码 function x2 = circfold(x1, N) %% Circular folding using DFT %% ----------------- ...
- 下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt。File构造函数中正确的路径和文件名的表示是( )。
下面的程序段创建了BufferedReader类的对象in,以便读取本机c盘my文件夹下的文件1.txt.File构造函数中正确的路径和文件名的表示是( ). ./表示当前项目的路径../表示当 ...
- LeetCode - Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- vue 插件tab选项卡(转载)
<template> <tab :options="tabOpt" :state.sync="stateIndex"></tab& ...
- lvm入门
实例: 使用lvm存储结构的主机需要扩容,现在我们已经将一个新的硬盘安装上去,将该新的硬盘的空间全部增加到主机上 20 ls /dev/sd* #查看新增加的硬盘名,我的为xvdb 21 ls /de ...
- linux网络编程概念(一)
AF表示地址族(address family) PF表示协议族(protocol family) domain参数 AF_UNIX 内核中通信 sockaddr_un AF_INET 通过ipv4 s ...
- Python的itertools模块
本章将介绍Python自建模块itertools,更多内容请参考:Python参考指南 python的自建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertool ...