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). ...
随机推荐
- 丢失libiconv-2.dll解决办法以及无法定位输入点libiconv-2.dll到动态链接库
摘自https://blog.csdn.net/mengxiangjia_linxi/article/details/78147348 丢失libiconv-2.dll解决办法以及无法定位输入点lib ...
- sqlmap开源 测试sql注入的工具 各种参考链接
https://www.cnblogs.com/insane-Mr-Li/p/10150165.html https://github.com/sqlmapproject/sqlmap 官网 http ...
- 123457123457#0#-----com.yuming.YiZhiFanPai01--前拼后广--益智早教游戏记忆翻牌cym
com.yuming.YiZhiFanPai01--前拼后广--益智早教游戏记忆翻牌cym
- python基础之坑爹正则表达式
python基础之坑爹正则表达式 概述 re模块就是python语言中的正则表达式,拆出来单独写一条blog是因为正则表达式本身就是比较庞大的知识,写具体些让自己以后方便查找. IP: ^(25[0- ...
- javascript模拟生成uuid
function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r ...
- (七)UML之用例图
用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. ...
- Node.js使用MySQL连接池示例
下面是一个封装好的工具类: var fs = require('fs'); var mysql = require('mysql'); var pool = mysql.createPool({ ho ...
- 最新 游族网络java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.游族网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了游族网络.6.7月主要是做系统复习.项目复盘.Leet ...
- zookeeper 操作命令
简介 查阅了网上相关资料,介绍zookeeper客户端命令并不是非常全面,大多数都是简单介绍ls.get.set.delete.stat这几个简单命令的,下面我把help中的所有命令简单介绍一下以供参 ...
- Java面试 - == 和 equals 的区别?
==:如果比较的对象是基本数据类型,则比较的是数值是否一致:如果比较的是引用数据类型,则比较的是对象的地址值是否一致. equals():equals()方法不能用于比较基本数据类型的对象,如果对象和 ...