6.container-with-most-water(装最多水的容器)
来源:互联网 发布:中文域名icp备案 编辑:程序博客网 时间:2024/05/20 09:06
6.container-with-most-water(装最多水的容器)
链接:http://www.lintcode.com/zh-cn/problem/container-with-most-water/
题目描述:
给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai)
。画 n 条垂直线,使得 i垂直线的两个端点分别为(i, ai)
和(i, 0)
。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。
注意事项
容器不可倾斜。
样例
给出[1,3,2]
, 最大的储水面积是2
.
分析:以序列最外面两条边形成的面基为起始面积,找出两条边中较小的一条,索引加一(i++
),找出一条更大的边来代替较小的边,以使得整个容器最大。
形象动图如下:https://leetcode.com/media/original_images/11_Container_Water.gif
class Solution {public: /** * @param heights: a vector of integers *@return: an integer */ int maxArea(vector<int> &heights){ // write your code here int len=heights.size(); int left=0,right=len-1,maxnum=0; while(left<right) { maxnum=max(maxnum,min(heights[left],heights[right])*(right-left)); if(heights[left]<=heights[right]) ++left; else --right; } return maxnum; }};
阅读全文
0 0
- Container With Most Water(能装最多水的容器)
- 6.container-with-most-water(装最多水的容器)
- Container With Most Water --装最多水的容器(重)
- LeetCode OJ 之 Container With Most Water(能装最多水的容器)
- 383.Container With Most Water-装最多水的容器(中等题)
- Leetcode刷题记——11. Container With Most Water(装最多水的容器)
- 383.Container With Most Water-装最多水的容器(中等题)
- Container With Most Water (容器中盛最多的水)
- 含水最多的容器(Container With Most Water)
- LeetCode | Container with most water(装最多的水)
- 11. Container With Most Water(求能装最多水的容器)
- Container With Most Water 装最多的水
- LeetCode 11. Container With Most Water(容器装水)
- Container With Most Water 求容器里能放最多的水 @LeetCode
- Container With Most Water 盛最多的水
- Container With Most Water容器盛水
- Container With Most Water(最大容器)
- LeetCode 11. Container With Most Water(盛最多的水)
- ASP.NET MVC 5实现基于Quartz.net 的任务调度管理平台(一)
- Ubuntu16.04安装ROS和turtlebot
- IIS发布Error——HTTP Error 503. The service is unavailable.
- #6 绑定元素属性
- DrawerLayout
- 6.container-with-most-water(装最多水的容器)
- 数据挖掘 h2o python随机森林
- CSDN物联网学习4 javascript的物联网应用开发
- BT5源码安装Python
- POJ3020 Antenna Placement
- 代码编程:(一)养成良好的编码习惯
- 用C实现将文件的内容读入内存
- 定义分销渠道(distribution channel)
- Python<H2o使用分布式随机森林建模分类>