题目:

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个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。找到两个线段,与x轴形成一个容器,使其包含最多的水。
备注:你不必倾倒容器。

这道题一开始没明白什么意思,想了很久,才想通,大致就是给你N条线段,例如a1,a2,a3,...,an,下标代表这条线段的位置,an代表这条线段的高度,找出两条线段,一高度最小的那条线段高度为长,两条线段的距离为宽,使这两条线段围成的正方形面积最大。比如 a1 = 10, a2 = 5, a3  = 6,则两两组合所围成的正方形面积分别为 (a1,a2) = 5 * (2 - 1) = 5, (a1,a3) = 6 * (3 - 1) = 12,(a2,a3) = 5 * (3 - 2) = 5,则该三条线段所围成的最大面积为12,则该题解为12。

解决这种问题我们可以通过暴力求解法,两两组合求出最大解,其算法为:

class Solution
{
public:int maxArea(vector<int> &height){int size = height.size();if (size <= 0)return 0;int result = 0;int tmpResult = 0;for (int i = 0; i < size-1; i++){for (int j = i+1; j < size; j++){if (height[i] >= height[j]){tmpResult = height[j] * (j - i);}else{tmpResult = height[i] * (j - i);}if (result < tmpResult)result = tmpResult;}}return result;}
};

该算法的时间复杂度为O(n*n),当然,我们提交后会显示Time Limit Exceeded;

另一个算法,如下所示:

class Solution
{
public:
int maxArea(vector<int> &height)
{
int i = 0;
int j = height.size() - 1;
int ret = 0;
while(i < j)
{
int area = (j - i) * min(height[i], height[j]);
ret = max(ret, area);
if (height[i] <= height[j])
i++;
else
j--;
}
return ret;
}
};

该算法的时间复杂度时O(n),其主要思想时从两边开始计算长方形面积,从线段最短的那边开始移动,每移动一次,就计算一次长方形面积,直到计算出最大面积。(只能移动最短的那条线,因为移动大的那条线,面积就更小了,那就找不到最大了。所以每次都是移动短的那条。

Container With Most Water-水桶装水问题相关推荐

  1. 【贪心+双指针】LeetCode 11. Container With Most Water

    LeetCode 11. Container With Most Water Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4455109.ht ...

  2. Container with most water(盛水最多的容器)

    Container with most water(盛水最多的容器) 简单的证明 如图 题目: 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n ...

  3. 多商户水站配送店员水票核销送水桶装水小程序开发

    多商户水站配送店员水票核销送水桶装水小程序开发 核心功能// 支持DIY页面.支持秒杀.支持分销.支持拼团.支持砍价.支持Vip会员.会员价.升级 .支持积分商城.支持充值送优惠券.代金券.礼品卷.计 ...

  4. leetcode -eleven:Container With Most Water

    2019独角兽企业重金招聘Python工程师标准>>> Given n non-negative integers a1, a2, ..., an, where each repre ...

  5. C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...

  6. 【LeetCode】11. Container With Most Water 解题小结

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  7. LeetCode 11.Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  8. LeetCode.11-装水最多的容器(Container With Most Water)

    这是悦乐书的第350次更新,第375篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第5题(顺位题号是11).给定n个非负整数a1,a2,-,an,其中每个表示坐标(i ...

  9. LeetCode 11 Container With Most Water

    问题:给出一个数组nums,要求选择2个,使得容器中包含的水最多. 思路:假设选取x,y,其中x<y,则包含的水为min(x,y)*(index(y) - index(x)).此时应该选择在最小 ...

  10. leetcode - Container With Most Water

    题目链接:http://oj.leetcode.com/problems/container-with-most-water/ 第一个解法是用两个数组双向记录.方法的时间复杂度是O(n+max).这里 ...

最新文章

  1. mysql使用参数指定用户_mysql-用户账号及权限管理
  2. js简单验证码的生成和验证
  3. 电缆的验证、鉴定和认证应该选择什么测试工具
  4. IdentityServer4-客户端的授权模式原理分析(三)
  5. angular jwt_Angular5 JWT身份验证(Spring Boot安全性)
  6. 一、Java Web——JDBC快速入门(详解)
  7. [KMP]一本通(http://ybt.ssoier.cn:8088) 1698:字符串匹配
  8. 孕妇能长期在计算机屏幕前工作吗,怀孕了在电脑前工作怎么办
  9. 用了Python,老板再也不用担心我写不了CUDA了!
  10. 拆除指令怎么设置_快捷指令(16)朗读屏幕内容
  11. 核心对象+持久对象全析(2)
  12. Axure RP Extension for Chrome经常损坏
  13. 代码整洁之道(二)优雅注释之道
  14. sql查询初学者指南_面向初学者SQL Server查询执行计划–非聚集索引运算符
  15. 一天一点linux(15):Ubuntu14.04 如何安装字体?
  16. 【HTTP请求】、详解
  17. Python打印2018年的日历(【问题描述】 打印2018年的日历 【输入形式】 【输出形式】 【样例输入】 【样例输出】)
  18. 如何在Word中打出罗马数字ⅠⅡ Ⅲ
  19. 音频格式怎么转换成mp3格式?
  20. float 精度探究

热门文章

  1. Ueditor中增加迅雷下载支持
  2. 3种局域网介质访问控制方法的比较
  3. 一元函数对象、一元谓词、二元函数对象、二元谓词
  4. 利用FlexCell实现的一些报表统计应用
  5. C#画布的创建和圆的画法
  6. 单周期CPU实验之学习之旅
  7. android 高德拖拽地图定位,拖拽选址-拖拽选址-示例中心-JS API UI 组件示例 | 高德地图API...
  8. 数字后端基本概念介绍Shape Blockage
  9. window7 安装TortoiseGit没有git.exe 和 右键没有clone等按钮解决方法
  10. java值面向对象2