【leetcode】Container With Most Water
题目描述:
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的更多相关文章
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【数组】Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- 【LeetCode】042 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
随机推荐
- RabbitMQ学习系列(二): RabbitMQ安装与配置
上一篇,简单介绍了RabbitMQ的情况还有一些相关的概念,这一篇,会讲讲 RabbitMQ安装与配置. 1.安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitM ...
- 关于编写性能高效的javascript事件的技术
如何能做出高效的web前端程序是我每次做前端开发都会不自觉去考虑的问题.几年前雅虎里牛逼的前端工程师们出了一本关于提升web前端性能的书籍,轰动了整个web开发技术界,让神秘的web前端优化问题成为了 ...
- RN8209校正软件开发心得(1)
最近领导突然让我做软件了,头大啊.以前也没怎么自己独立做过软件,这次来的突然啊,面对这么大的任务量自己只能加把劲了,还等着领导给涨工资呢,哈哈... 作为编程的小白,要自己做一款上位机的软件实属不易啊 ...
- python基础知识(三)
摘要:主要涉及新数据类型set集合.三元运算.深浅拷贝.函数基础.全局变量与局部变量 一.set --> 无序,不允许重复的集合 不允许重复的列表, 1,创建 s = set() 接收 ...
- Visual Studio EventHandler Delegate 和 EventArgs
EventHandler代理 用来表示处理一个没有事件数据(event data)的事件(event)的 方法. 无论何时事件发生时,事件代理就被调用来触发以前事件驱动的其他事件(监听当前事件TCur ...
- iOS 浅谈:深.浅拷贝与copy.strong
深.浅拷贝 copy mutableCopy NSString NSString *string = @"汉斯哈哈哈"; // 没有产生新对象 NSString *copyStri ...
- svn检出的时候报 Unable to connect to a repository at URL错误(摘自CSDN)
背景:1. SVN服务器:VisualSVN-Server-2.5.5: 2. SVN客户端:TortoiseSVN-1.7.6.22632-x64-svn-1.7.4.msi: 在S ...
- Struts2第一个入门案例
一.如何获取Struts2,以及Struts2资源包的目录结构的了解 Struts的官方地址为http://struts.apache.org 在他的主页当中,我们可以通过左侧的Apache ...
- Wix打包技术学习笔记
http://blog.csdn.net/duanzilin/article/details/5951709 很好的教程,有时间好好学习一下.然后自己整理笔记,暂时不打算深入研究
- express-session使用理解
本篇主要段落翻译来自https://github.com/expressjs/session 客户端与服务会使用一个Sessionid的Cookie值来进行客户端和服务器端会话的匹配,这个Cookie ...