题目描述:

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. JVM内存垃圾回收方法

    1.概述 1.1.为什么要回收? 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断地分配内存空间而不进行回收.除非内存无限大,我们可以任性地分配而不回收,但是事实并非如此.所以,垃圾回收是必须 ...

  2. Lua IDE

    http://blog.csdn.net/visualcatsharp/article/details/37653107

  3. Parse Fatal Error at line 4 column 43: 已经为元素 "web-app" 指定属性 "xmlns"。

    打开web.xml;最上面的web-app里面,看有没有重复的,重点关注xmlns="http://java.sun.com/xml/ns/javaee" ,如果重复,删去就好~~ ...

  4. ASP.NET知识总结 (未完) 本人新手

    1:HTTP的工作方式是什么?1)客户端提交表单请求处理 Request2)服务器端处理程序进行处理 Handle3)服务端相应 Response 2:1)get(默认值)是通过地址栏的URL显式地传 ...

  5. [转]SpringMVC Controller介绍及常用注解

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  6. Mac OS X 中一些常用的命令行技巧

    一.网络设置相关 1.网卡的物理地址的动态重置 出于某些需求,例如网络中的 IP 地址或网络帐号与网卡物理地址绑定,使得多个设备无法切换上网,可尝试临时更改物理地址.不过,系统偏好设置是不能修改网卡物 ...

  7. MAC实用的小工具

    一.XtraFinder(右键菜单扩展) http://www.xuebuyuan.com/173454.html http://www.mamicode.com/info-detail-111618 ...

  8. IIS 配置MVC项目路由中以api结尾的接口

    公司的接口,前端框架是MVC,路由里面对接口配置的路径是以api结尾的,结果之间在iis上访问不了,研究了半天成功了,整理下,以免下次再忘. 路由配置RouteConfig routes.MapRou ...

  9. 《转载》Java异常处理的10个最佳实践

    本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...

  10. CornerStone的使用

    俗话说:"工欲善其事必先利其器": 对于我们程序员来说,不管你是大神,还是小鱼小虾,进入公司之后,都用过源码管理工具,不然你就不是一个合格的程序员,现在各个公司用于源码管理工具通常 ...