题目描述:

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. php实现返回上一页的功能

    php实现返回上一页的功能的3种有效方法 header(location:你的上一页的路径);   //   注意这个函数前不能有输出      header(location:.getenv(&qu ...

  2. matlab下二重积分的蒙特卡洛算法

    %%monte_carlo_ff.m %被积函数(二重) function ff=monte_carlo_ff(x,y) ff=x*y^2;%函数定义处 end %%monte_carlo.m %蒙特 ...

  3. 感悟:搞了整整一天,拯救一个Ubuntu系统

    最开始,我在windows平台上,准备打开计算机组成原理的实验工程,突然来了一个想法:每次要用windows的时候,都要切换系统(win8以上的系统必须在关闭快速开机的状态下才能正常在Linux平台下 ...

  4. Eclipse MySQL Hibernate 中文乱码问题

    如果是Eclipse的问题,那就对以下各项进行设置,即可排除问题. 设置eclipse相关编码为UTF-8: 修改工作区默认编码,位置:windows--perferences--general--w ...

  5. for和foreace的区别

    foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreach语句并不能完全取代for语 ...

  6. 设备旋转,创建水平模式布局--Android studio

    1.在项目工具窗口中,右键单击res目录后选择new--Android resource directory菜单项. 2.从资源类型Resource type列表中选择layout,保持Source ...

  7. c++文件输入输出流fstream,对输入>>和输出<<重载

    1. fstream 继承自iostream --> 要包含头文件#include<fstream> 2. 建立文件流对象 3. 打开文件夹 4. 测试是否打开成功 5. 进行读写操 ...

  8. nmq消息队列解析

    消息中间件NMQ 1.What is nmq? nmq = new message queue; 一个通用消息队列系统 为在线服务设计 什么是消息队列?问什么需要?有哪些功能? 消息队列的本质:1.多 ...

  9. c++ map、vector、list

    总体来说,使用map最简单.支持查找,获取下标不存在也不会出错 map是使用rbtree结构, vector是用连续获取内存的方法,类似hash结构.list是链表结构, 不支持下标. map: 支持 ...

  10. <hr>标签不止创建html水平线也可以画圆噢

    看到上面这张图,第一反应是用photoshop类似作图软件画出来的,但那是华丽丽的错觉.一个简单的<hr>标签就能完成漂亮的效果,而不仅仅是创建html水平线.想知道怎么实现吗?Let's ...