【LeetCode】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.
一般的方法就是嵌套两个for循环,不过提交的时候就超时了。那么换个角度考虑,先从最宽的情况,也就是height.size()-1到0这个横坐标之间最大的水容量,比较两者的an-1和a0哪个小一些。然后逐渐往中间靠拢,只有比min(an-1,a0)高的才会被考虑,计算水容量大小,如果比之前大,则更新axis。
class Solution { public:int maxArea(vector<int>& height) {int tmpArea = 0;int i = 0, j = height.size()-1;while (i < j){int h = height[i] > height[j] ? height[j]:height[i];tmpArea = h * (j - i) > tmpArea ? h * (j - i) : tmpArea;while (height[i] <= h && i < j)i++;while (height[j] <= h && i < j)j--;}return tmpArea;} };
转载于:https://www.cnblogs.com/Doctengineer/p/5813127.html
【LeetCode】11. Container With Most Water 解题小结相关推荐
- 【贪心+双指针】LeetCode 11. Container With Most Water
LeetCode 11. Container With Most Water Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4455109.ht ...
- LeetCode 11 Container With Most Water
问题:给出一个数组nums,要求选择2个,使得容器中包含的水最多. 思路:假设选取x,y,其中x<y,则包含的水为min(x,y)*(index(y) - index(x)).此时应该选择在最小 ...
- LeetCode 11.Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode 11. Container With Most Water
题意:题目大概意思就是让你找两个数使得两个数的最小值乘以他们的距离最大Area 题解:设置两个变量, 一个是从头开始,一个是从尾开始,计算两个数的Area,并更新最大值,如果开始的元素大于结尾的元素则 ...
- LeetCode 11. Container With Most Water--Java 解法--困雨水简单版
LeetCode 11. Container With Most Water–Java 解法 此文首发于我的个人博客:LeetCode 11. Container With Most Water–Ja ...
- leetcode -eleven:Container With Most Water
2019独角兽企业重金招聘Python工程师标准>>> Given n non-negative integers a1, a2, ..., an, where each repre ...
- 11. Container With Most Water
2019独角兽企业重金招聘Python工程师标准>>> 就是说,x轴上在1,2,...,n点上有许多垂直的线段,长度依次是a1, a2, ..., an.找出两条线段,使他们和x抽围 ...
- LeetCode第45场双周赛-解题报告
LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...
- [贪心|双指针] leetcode 11 盛最多水的容器
[贪心|双指针] leetcode 11 盛最多水的容器 1.题目 题目链接 给你 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 ...
最新文章
- Java中break、Continue、reutrn总结
- 图像传感器与信号处理——自动曝光算法
- pytorch基础知识整理(三)模型保存与加载
- 如何轻松拿到大厂面试offer | 掘金技术征文
- 需要按次序点击链接的网页特效
- 小程序 --- 点击放大功能、获取位置信息、文字样式省略、页面跳转(navigateTo)
- MFC让窗口最前端显示
- manjaro linux下查看硬件温度
- linux服务器选择u盘启动,【服务器运维】linux怎样以u盘启动
- 至强服务器性能排行,英特尔至强处理器排名天梯 至强cpu天梯2020排名
- 为什么我不推荐去SAP培训机构参加培训?
- vim 退格键(backspace)不能用
- python_操作MySQL 初解 之__类方法调用并 增-删-改-查
- 信息系统项目管理师第四版(最新)重点(背诵)内容概览
- VMWare 设置CentOS虚拟机固定IP地址
- java变量_Java变量
- 我如何在咨询项目中使用Vagrant和Docker
- 学习:数学----欧拉定理与扩展欧拉定理
- obsidian第三方插件无法加载
- 【LOJ 6485】LJJ 学二项式定理(单位根反演)(模板)
热门文章
- android activity焦点,android启动activity文本框不获得焦点
- oracle grid安装看不到config,【图片】【rac11g安装问题】出了问题特来请教各位大侠【oracle吧】_百度贴吧...
- java 泛型参数_[基础]-Java泛型
- python logging模块详解_python logging模块使用总结
- .net函数查询_用python实现多文档查询
- chart.js雷达图不显示刻度标签_Matplotlib从入门再也不放弃
- 微信小程序引入骨架屏组件
- 数据库主键从某个值开始自增
- python tkinter计算器实例_Python+tkinter使用80行代码实现一个计算器实例
- 非命令行linux安装zip,如何在Ubuntu 18.04 Bionic Beaver Linux上使用命令行从ZIP文件安装Gnome Shell Extensions...