题目描述:

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. Oracle创建自增ID

    先创建序列sequence create sequence S_User minvalue 1 nomaxvalue  -- 或 maxvalue 999 start with 1 increment ...

  2. zabbix监控Java 8080端口

    linux下端口和服务是对应的,Java进程启动时默认监听8080端口,如果服务挂掉则8080端口就没有了. lsof -i:8080 端口,如果没有任何的输出,说明该端口不在工作. 想在zabbix ...

  3. 请注意,再次记住, centos7,fedora 24中 没有iptables服务, 而使用的firewalld, 也可以安装 iptables-services程序来实现

    原来写了一篇文章的, 忘了: http://www.cnblogs.com/bkylee/p/5837481.html 可以用 firewall-cmd 这个命令来管理 防火墙...

  4. 网页插件学javascript还是jquery好啊?

    文章的起因,也是在群内交流是回答一个小伙的问题,一扯就停不下来,但由于个人知识面覆盖有限,自身基础又不够扎实,仅供参考: 问这个问题之前,我个人建议先搞清什么是jquery,什么是js?     jq ...

  5. Swift与OC区别

    一.Swift与OC区别: 1.swift程序的入口是UIApplicationMain; 2.OC的类是以.h和.m组成的;swift是一.swift结尾的; 3.OC的类是以@interface和 ...

  6. JavaScript - 正则表达之二

    正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...

  7. dotNet平台模板列中的单选无效的解决方案

    最近在grid里添加一个单选列,最开始直接创建一个模板列,然后在模板列里放一个radiobutton.并指定其GroupName.这是radiabutton最常用的方法.但是在Grid里,这样却毫无效 ...

  8. Azure上的那些IP

    相信第一次接触Azure的读者都会碰到这样一个问题,就是Azure的IP地址,笔者第一次接触Azure也是被搞懵逼了,一会儿VIP,不知道的还以为是会员的意思呢,一会儿又是DIP,后来又来了个PIP, ...

  9. Linux下Python 文件内容替换脚本

    Linux下Python 文件替换脚本 import sys,os if len(sys.argv)<=4: old_text,new_text = sys.argv[1],sys.argv[2 ...

  10. AMD电脑装完Winsows10后开机蓝屏,报错代码:cdmsnroot_s.sys

    背景:今天装了个WIN10,电脑配置:联想 IdeaPad   Z485      : AMD   A8处理器      .完成安装后电脑没有问题,安装了驱动程序后将           电脑用360 ...