题目

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. springboot快速入门(五)——事务管理

    一.入门 概念就不再赘述了,由于一般我们是通过service控制事务,这里给出注解式的示例: package com.example.demo; import com.example.demo.bea ...

  2. Redis学习之路(二)之Redis入门基础

    一.Redis基本介绍 (1)Redis介绍 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(string ...

  3. 实战中的asp.net core结合Consul集群&Docker实现服务治理

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.前言 在写这篇文章之前,我看了很多关于consul的服务治理,但发现基本上都是直接在powershell或者以命令工具的方式在 ...

  4. JavaScript中的null和undefined

    null :表示无值;undefined : 表示一个未声明的变量,                或已声明但没有赋值的变量,                或一个并不存在的对象属性. ==运算符将两 ...

  5. 发现一个很N且免费的html5拓扑图 关系图 生成组件

    传送门:http://visjs.org/ demo代码 <!doctype html> <html> <head> <title>vis.js new ...

  6. 私有Docker仓库login Error response from daemon: Get https://x.x.x.x/v2/: dial tcp x.x.x.x:443: connect: connection refused

    一.登陆私有仓库错误: docker login --username=evan 192.168.0.203 Error response from daemon: Get https://192.1 ...

  7. sublime text 安装json插件

    1.安装JSON插件(直接安装或在线插件安装均可) 1-1.直接安装 下载安装包https://github.com/dzhibas/SublimePrettyJson,解压缩到Packages目录( ...

  8. Android Studio —— java.lang.VerifyError: Verifier rejected class 问题解决

    用as开发过程中遇见问题app闪退  log如下图: 解决: File—Settings: 点击Apply—OK重新运行即可

  9. Ubuntu解压zip包中文乱码

    解决方法:通过unar 工具解压 步骤一: 安装unar: sudo apt-get install unrar 步骤二: 解压(以test.zip为例):unar test.zip 解压成功,乱码问 ...

  10. IIS6/IIS7环境下实现支持mp4视频随意拖动、预览播放、边下载边播放

    前几天,一客户需要在IIS环境下实现MP4视频可以随意拖动观看,边下载边播放.一看这要求,IIS本身是无法实现,想着应该需要用插件,于是GG一番,还真找到这样的插件,此组件为H264-Streamin ...