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). ...
随机推荐
- SSM基于Token的登录认证
1.什么是token token的意思是“令牌”,是服务端生成的一串字符串,作为客户端进行请求的一个标识. 当用户第一次登录后,服务器生成一个token并将此token返回给客户端,以后客户端只需带上 ...
- 阿里fastjson工具类
package com.common.utils.jsonUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JS ...
- CentOS7下搭建zabbix监控(三)——Zabbix监控服务配置
CentOS7下搭建zabbix监控(一)——Zabbix监控端配置 CentOS7下搭建zabbix监控(二)——Zabbix被监控端配置 (1).配置Zabbix监控Apache服务 主机名:yo ...
- tcpdump抓包代码
tcpdump - tcp[:]=:]=0x4854 or tcp 抓出来的包可以导入wireshark分析 以上代码曾经在ios越狱机器上使用,用于抓包,具体也记不起来了 导入wireshark效果
- sonarqube-jenkins-config
Sonar Config .Jenkinsfile config stage('SonarQube analysis') { steps { script { scannerHome = tool ' ...
- iOS compile sources问题
以前xcode compile sources 这里是自动生成的,升级到新版本之后,我删掉这里的列表,就没法生成了.导致项目无法运行. 解决方法就是:自己手动添加.h文件
- 第二十二章 集成验证码——《跟我学Shiro》
目录贴:跟我学Shiro目录贴 在做用户登录功能时,很多时候都需要验证码支持,验证码的目的是为了防止机器人模拟真实用户登录而恶意访问,如暴力破解用户密码/恶意评论等.目前也有一些验证码比较简单,通过一 ...
- celery的log如何传递给django,由django管理
celery自己管理log目录 celery worker --autoscale=4,1 --app=erebus.celeryapp:app -l info -f /home/admin/outp ...
- Matlab批量获取子文件夹与文件名
批量得到父文件夹下所有子文件夹的路径的MATLAB函数. function [SubFolders] = GetFolders(ParentFolder) %GetFolders % 函数功能为获取父 ...
- VS2010 如何在调试的时候输入参数
VS2010 如何在调试的时候输入参数 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 很明显,好多算法程序实现的时候提供的例程都是需要在命令行中输入参数,比 ...