Array+DP leetcode-11.装更多的水
11. Container With Most Water
题面
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) 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.装更多的水的更多相关文章
- 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(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板
题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...
- 2021.11.30 eleveni的水省选题的记录
2021.11.30 eleveni的水省选题的记录 因为eleveni比较菜,eleveni决定先刷图论,再刷数据结构,同时每天都要刷dp.当然,对于擅长的图论,eleveni决定从蓝题开始刷.当然 ...
- 2021.11.02 eleveni的水省选题的记录
2021.11.02 eleveni的水省选题的记录 因为eleveni比较菜,所以eleveni决定从绿题开始水 --实际上菜菜的eleveni连绿题都不一定能水过/忍不住哭了 [P2217 HAO ...
- [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). ...
- 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 ], 这样 ...
- 2021.11.05 eleveni的水省选题的记录
2021.11.05 eleveni的水省选题的记录 因为eleveni比较菜,但是eleveni不想写绿题(总不能说是被绿题虐得不想写),eleveni决定继续水noip原题. --实际上菜菜的el ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
随机推荐
- 处理线上CPU负载过高的故障现象
如何处理线上CPU100%的故障现象 处理流程: 1.登陆线上机器top命令,查看耗费cpu的进程号,举例来说发现进程24008持续耗费资源 2.top -Hp 24008去查看持续耗费cpu的线程号 ...
- 123457---com.threeObj.Baobaoshizi01--- 宝宝识字01
com.threeObj.Baobaoshizi01--- 宝宝识字01
- JSONP实现Ajax跨域请求
前言 由于浏览器存在同源策略的机制,所谓同源策略就是阻止从一个源(域名,包括同一个根域名下的不同二级域名)加载的文档或者脚本获取/或者设置另一个源加载的文档属性. 但比较特别的是:由于同源策略是浏览器 ...
- Tips for vcpkg
概述 vcpkg是微软开发的在Windows, Linux和MacOS平台管理C/C++库的开源工具. 快速开始 要求 使用vcpkg需满足如下条件: Windows 10, 8.1, 7, Linu ...
- js动态改变iframe的高度
js动态改变iframe的高度的写法 〈iframe id="docDetail" width="100%" height="200" ...
- 利用windows任务计划备份sqlserver
sqlserver Express版不含agent service(虽然可以从“服务”里看到,但是无法启动),因此无法使用代理服务执行备份作业,如果代理作业服务正常,建议还是首先采用代理服务. 此备份 ...
- npm镜像指定用淘宝镜像去下载
使用npm下载,蜗牛,使用cnpm又觉得那啥,所以.把cnpm也就是淘宝镜像绑定成npm下载的代理,这样使用npm的时候其实是用淘宝镜像去下载,这感觉,good! 1. npm config set ...
- windows服务器入门 mysql的安装
钱的问题 只能安装mysql了 下载MySQL 5.5(其他的版本都可以的 只是在这里以mysql5.5为例)安装包(下载地址:http://dev.mysql.com/downloads/my ...
- cocos creator 实现打字机的效果
作为一个萌新,我只想说我是没有感情的粘贴工具! let richText = this.viewNode.getChildByName('richText').getComponent(cc.Rich ...
- python 创建虚拟环境时报错OSError, setuptools下载失败
错误信息如下: Using base prefix 'c:\\users\\huful\\appdata\\local\\programs\\python\\python36-32'New pytho ...