11. Container With Most Water

题面

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

给定n个非负数字,代表n条垂线,在垂线之间装水,找出能装水最多的情况。

样例(如上图)

Example:

Input: [1,,6,2,5,4,8,3,]
Output: 49
statement: (8-1)*min(8, 7)

思路1:

暴力法:遍历所有可能情况,找出最大值。

时间复杂度:O(n2)

空间复杂度:O(1)

 class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
if(size == )
return min(height[], height[]);
int res = ;
for(int i=; i<size; i++)
{
for(int j=i+; j<size; j++)
{
int tmp = (j-i)*min(height[j],height[i]);
if(tmp > res)
res = tmp;
}
}
return res;
}
};

思路2:

两点逼近思想

1. 先取最左端和和最右端,然后计算容积,更新容积;

2. 更新l and r, 那一端长度height[]短,那一段就更新(++/--);

3. 输出最大容积。

时间复杂度:O(n)

空间复杂度:O(1)

 class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
if(size == )
return min(height[], height[]);
int res = ;
int l =, r = size-;
while(l < r)
{
int tmp = (r-l)*min(height[r],height[l]);
if(tmp > res)
res = tmp;
if(height[l] > height[r])
r--;
else
l++;
}
return res;
}
};

Array+DP leetcode-11.装更多的水的更多相关文章

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

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

  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. 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板

    题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...

  4. 2021.11.30 eleveni的水省选题的记录

    2021.11.30 eleveni的水省选题的记录 因为eleveni比较菜,eleveni决定先刷图论,再刷数据结构,同时每天都要刷dp.当然,对于擅长的图论,eleveni决定从蓝题开始刷.当然 ...

  5. 2021.11.02 eleveni的水省选题的记录

    2021.11.02 eleveni的水省选题的记录 因为eleveni比较菜,所以eleveni决定从绿题开始水 --实际上菜菜的eleveni连绿题都不一定能水过/忍不住哭了 [P2217 HAO ...

  6. [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).  ...

  7. Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题

    F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...

  8. 2021.11.05 eleveni的水省选题的记录

    2021.11.05 eleveni的水省选题的记录 因为eleveni比较菜,但是eleveni不想写绿题(总不能说是被绿题虐得不想写),eleveni决定继续水noip原题. --实际上菜菜的el ...

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

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

随机推荐

  1. jmeter 查看结果树,获取响应体写法校验是否提取写法是否正确的方法

    JSON Path Expression里面写入提出值的写法,点击Test测试提取

  2. MySQL安装时出现Apply Security Settings错误的解决办法(转)

    最近在学习MySQL时,下载了MySQL5.5版本的安装包,在配置向导的最后的页面却出现了Apply Security Settings的错误.第一次安装时比较顺利,中途卸载了一下,结果第二次安装的时 ...

  3. python面向对象之初步认识

    面向对象 类,用来描述一类事物的相同的特征或者属性.比如说,狗,狗属于一种统称,狗还分有不同的种类,比如,牧羊犬,蝴蝶犬,京巴等等,他们都有相同的特征,一个头,两个耳朵,四条腿,会跑,会吃东西,会汪汪 ...

  4. 【ARTS】01_34_左耳听风-201900701~201900707

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  5. UI界面测试

    概念:指测试用户界面的风格是否满足用户要求.文字是否正确.页面是否美观.文字与图片组合是否完美.操作是否友好等. 1.窗体测试 { 窗体大小. 移动窗体. 缩放窗体. 显示分辨率. 状态栏. 工具栏. ...

  6. ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法

    这个是非常常用的案例,ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法/** * 单文件上传 * name:表单上传文件的名字 * ext: 文件允许的后缀,字符串形式 * path:文件保 ...

  7. C#基础知识学习 三

  8. linux网卡出现问题:Job for network.service failed because the control process exited with error code问题

    [转自]:https://blog.csdn.net/dongfei2033/article/details/81124465 今天在centOS 7下更改完静态ip后发现network服务重启不了, ...

  9. Netty中两种Keepalive的区别

    在Server端开启TCP keepalive: 两种方式 serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); serverB ...

  10. Python 【类与对象】

    类与对象 把类的个例就叫做实例 (instance),可理解为“实际的例子”类是某个特定的群体,实例是群体中某个具体的个体 Python中的对象等于类和实例的集合:即类可以看作是对象,实例也可以看作是 ...