题目描述:

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.

解题思路:

这道题说白了就是选两个值中较小的一个\(min(a_i,a_j)\),然后乘以他们之间的间距\((i-j)\)的最大值.最简单的\(O(n^2)\)的遍历。但是我们可以采取贪心的算法,从两端开始不断选择较大的一侧组成新的container。

class Solution:
# @return an integer
def maxArea(self, height):
res = 0
l = len(height)
i = 0
j = l-1
while i < j:
t = min(height[i], height[j]) * (j-i)
if t > res:
res = t
if height[i] < height[j]:
i += 1
else:
j -= 1
return res

【leetcode】Container With Most Water的更多相关文章

  1. 【leetcode】Container With Most Water(middle)

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

  2. 【数组】Container With Most Water

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

  3. 【LeetCode】42. Trapping Rain Water 接雨水 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...

  4. 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...

  5. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  6. 【LeetCode】042 Trapping Rain Water

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

  7. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  8. 【一天一道LeetCode】#42. Trapping Rain Water

    一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...

  9. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

随机推荐

  1. Windows下安装Oracle拖慢开机速度的解决方法

    环境:win7 + oracle R2 方法:将安装Oracle后自动开机启动的服务改为手动启动 步骤如下: 1.修改服务项 Ctrl + R,输入services.msc,打开服务列表,找到Orac ...

  2. ss命令和Recv-Q和Send-Q状态

    ss 用来显示处于活动状态的套接字信息.ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比nets ...

  3. 移动端Web开发调试之Chrome远程调试(Remote Debugging)

    比如手机钉钉调试页面,下面是一位同学整理的链接: http://blog.csdn.net/freshlover/article/details/42528643/ 如果inspect 后,一直空白, ...

  4. Could not find or load main class org.gradle.wrapper.GradleWrapperMain解决办法

    解决办法: gradlew is the gradle wrapper executable - batch script on windows and shell script elsewhere. ...

  5. 拖动对象ondrag

    说明: 在进行拖放操作时,dataTransfer 对象用来保存被拖动的数据.它可以保存一项或多项数据.一种或者多种数据类型.dataTransfer对象有两个主要的方法:getData()方法和se ...

  6. [译]IIS 8.0应用初始化

    原文 代码 或者点这 通过IIS 8.0应用初始化特性管理员可以配置IIS为一个网站或多个网站提前执行初始化任务.当应用在初始化期间,可以通过配置先返回一个静态页面知道应用的初始化任务完成. 通过配置 ...

  7. 腾讯开放平台web第三方登录获取信息类(包含签名)

    不清楚具体参数的可以先看下第三方登录的文档: class QQ { //$appid 你的appid //$openid 获取到的唯一的用户openid //$openkey 获取到的openkey ...

  8. 项目经理排期的几个tip

    互联网敏捷开发的流程可以演绎出很多玩法,但最好都遵循以下几个tip: 1, 对项目经理最重要的一条是,通读所有需求文档,跟相关PM进行彻底沟通,把需求都搞熟搞透,防止漏排任务: 2, Stroy拆分要 ...

  9. 总结初用erlang 时的遇到一些问题

    算起来接触erlang 三个多月快四个月来,期间从零开始看书写erlang代码.修改RabbitMQ.业务开发.系统调优,总算是有点入门了. 最难受的是边学边修改RabbitMQ,开始真心的看不懂,不 ...

  10. Socket通信功能实现

    前段时间写的小功能,包含了较为详细的代码注释,需要的可以下载看下.对你有一点用,请赞一个~~ http://www.cnblogs.com/zhili/category/397082.html  这里 ...