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.

Example:                       Input: [1,8,6,2,5,4,8,3,7]                   Output: 49

思路


  我们可以设置两个指针,一个指向开始位置,一个指向尾部。然后求出当前储水量,然后将短的一方向前移动,然后继续算储水量。并且和之前的进行比较。大于替换,否则直接下一步。 时间复杂度O(n), 空间复杂度为O(1)。

图示步骤


      

 

     

     

代码


 class Solution(object):
def maxArea(self, height):
if len(height) < : # 长度小于2直接返回
return
start, end = , len(height)-1
areas =
while start < end: # 开始循环比较
tem = min(height[start], height[end]) # 选出最小的数字
areas = max(areas, tem*(end - start)) # 与之前的储水量进行比较
if height[start] < height[end]: # 选择移动的指标
start +=
else:
end -=
return areas

【LeetCode每天一题】Container With Most Water(容器中最多的水)的更多相关文章

  1. leetcode第11题--Container With Most Water

    Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  2. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

  3. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

  4. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

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

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

  6. 【LeetCode two_pointer】11. Container With Most Water

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

  7. LeetCode 笔记系列二 Container With Most Water

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

  8. LeetCode(11) Container With Most Water

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

  9. 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

随机推荐

  1. 在最新的Power BI Desktop中使用报表主题功能

    报表主题的工作原理 Power BI Desktop中,增加了万众期待的报表主题色功能(预览),如下图: 要使用这个功能,请先在官网下载并安装最新版的Power BI Desktop.安装成功后在&q ...

  2. MySQL 环境搭建之解压方式安装

    一 .MySQL服务 安装 1.下载:  地址: http://dev.mysql.com/downloads/mysql/ 2.安装: 将下载的mysql-5.7.16-winx64压缩包解压后的整 ...

  3. 常用Common集合

    Log using System; using System.IO; using System.Linq; using System.Text; namespace Common { public c ...

  4. 洛谷 P1208混合牛奶【贪心】

    题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...

  5. sess.run(tf.global_variables_initializer()) 做了什么?

    当我们训练自己的神经网络的时候,无一例外的就是都会加上一句 sess.run(tf.global_variables_initializer()) ,这行代码的官方解释是 初始化模型的参数.那么,它到 ...

  6. Improved SEO with mod_rewrite

    PHP Advanced and Object-Oriented Programming Third Edition <?php //D:\wamp64\www\0613pm\w_wwwroot ...

  7. elasticsearch ingest node and docker-cluster---quey using sql]

    es-docker-cluster https://stefanprodan.com/2016/elasticsearch-cluster-with-docker/ https://github.co ...

  8. 如何获取Android系统APP的Package Name和Activity Name

    有两种方式: 方式一.aapt.exe查看Package Name和入口Activity Name (1) 在安装路径android-sdk\platform-tools下查找aapt.exe:  如 ...

  9. 机器学习:K-Means/K-Means++

  10. Python摸爬滚打之day04----基本数据类型(列表,元组)

    1.列表 列表是可变的, 有序的数据类型,列表是按照添加顺序来保存的,可以存放各种数据类型. 1.1    列表的切片(同字符串) 1.2    列表的增删改查 注意: 列表是可以直接在列表上面进行操 ...