题目

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 and n is at least 2.

翻译

给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点。绘制n条垂直线,使得线i的两个端点处于(i,ai)和(i,0)。找到两条线,它们与x轴一起形成容器,使得容器含有最多的水。

Hints

Related Topics: Array, Two Points

容器是有木桶效应的 也就是说短的垂直线决定了整个容器的高 所要求的是 max(短边*坐标差)

找到一些最大值的一般想法是通过可能发生最大值的所有情况,并继续更新最大值。为了提高扫描效率,要找到一种智能的扫描方式来切断无用的情况,同时100%保证通过其他情况可以达到最大值

这儿可以通过设置左右两端的初始指针,向中间扫描,每次只移动较小值的指针 每一次移动都更新最大值

代码

Java

class Solution {
public int maxArea(int[] height) {
int left,right;
left = 0;right = height.length-1;
int maxArea = 0;
while(left<right){
maxArea = Math.max(maxArea, (right-left)*(Math.min(height[left],height[right])));
if(height[left]<height[right])
left++;
else
right--;
}
return maxArea;
}
}

Python

class Solution(object):
def maxArea(self, height):
l = 0; r = len(height)-1;
maxArea = 0
while l<r:
maxArea = max(maxArea, (r-l)*min(height[l],height[r]))
if height[l]<height[r]:
l += 1
else:
r -= 1
return maxArea

蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]

    题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...

  2. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  3. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  4. # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  5. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  6. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  7. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  8. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  9. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

随机推荐

  1. 四 Hive整合HBase

    安装环境: hbase版本:hbase-1.4.0-bin.tar.gz hive版本:   apache-hive-1.2.1-bin.tar 注意请使用高一点的hbase版本,不然就算hive和h ...

  2. target属性用于返回最初触发事件的DOM元素。

    target属性用于返回最初触发事件的DOM元素. 在HTML文档中,我们为<p>元素绑定点击事件("click"),由于DOM元素的事件冒泡机制,我们点击<p& ...

  3. 【轮子狂魔】抛弃IIS,打造个性的Web Server - WebAPI/Lua/MVC(附带源码)

    引言 此篇是<[轮子狂魔]抛弃IIS,向天借个HttpListener - 基础篇(附带源码)>的续篇,也可以说是提高篇,如果你对HttpListener不甚了解的话,建议先看下基础篇. ...

  4. 如何用Fiddle工具实现手机抓包

    Fiddler官方下载地址:http://fiddler2.com/ 在做手机或移动端APP的接口测试时,我们可以使用fiddler对APP进行抓包确认,抓取对应的网络交互信息.在抓取的信息中可以看到 ...

  5. Windows下Redis集群搭建

    1.第一步先安装Redis 参照<Windows下Redis安装及使用.docx> 在Redis目录E:/Redis下新建Logs文件夹,并且创建3个端口下的配置文件,记得修改里面的接口 ...

  6. RabbitMQ入门:Hello RabbitMQ 代码实例

    在之前的一篇博客RabbitMQ入门:认识并安装RabbitMQ(以Windows系统为例)中,我们安装了RabbitMQ并且对其也有的初步的认识,今天就来写个入门小例子来加深概念理解并了解代码怎么实 ...

  7. vim文本编辑工具(全)

    VIM文本编辑工具 编辑模式 i    在当前字符前插入I   在光标所在的行首插入a 在当前字符后插入A 在光标所在行尾插入o 在当前行的下一行插入新的一行O 在当前行的上一行插入新的一行 s   ...

  8. webpack3升级为webpack4

    写在前面的话:为什么要升级,因为公司目前的项目使用webpack3,但是因为是多页应用,入口估计有一百多个,开发模式下慢得不像话,修改一个文件需要十几秒才编译好,之前的解决方案是减少入口,但是要调试其 ...

  9. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第7节: 获取异线程释放的对象

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第七节: 获取异线程释放的对象 上一小节分析了异线程回收对象, 原理是通过与stack关联的WeakOrder ...

  10. 【Docker】第三篇 Docker容器管理

    一.Docker容器概述: 简单理解容器是镜像的一个实例. 镜像是静态的只读文件,而容器的运行需要可写文件层. 二.创建容器 [root@web130 ~]# docker create -it ub ...