前言

难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了

题目

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.



思路

首先,我需要模拟出这个场景,推荐一个画图软件:
http://www.processon.com,虽然很小众,但是确实很好用




当从两边向中间考虑时,乘水的面积是由(两端较小的高度)×(两个板之间的宽度)决定的。记录最开始的乘水面积为ans1,然后L向右运动,R向左运动,截止条件是L >= R,并且记录乘水的面积ans,取最大值

以L向左运动为例,当宽度减小时,如果面积变大,必然高度要增加,因此L只需取比前一个L大的值即可,初始L的高度为L1。R向右运动同理


AC代码

public class Solution {
public int maxArea(int[] height) {
int i, j, lh, rh, area, tmp, len = height.length; lh = height[0];
rh = height[len - 1];
area = Math.min(height[0], height[len - 1]) * (len - 1); for (i = 1, j = len - 2; i < j;) {
while (i < j && height[i] <= lh) {
i++;
}
if (i < j) {
lh = height[i];
} while (j < j && height[j] <= rh) {
j--;
}
if (i < j) {
rh = height[j];
} tmp = Math.min(lh, rh) * (j - i); if (tmp > area) {
area = tmp;
}
} return area;
}
}



[LeetCode]Container With Most Water, 解题报告的更多相关文章

  1. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  5. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. 使用Android SDK Manager自动下载速度慢解决方法

    可以在SDK Manager 的更新界面,勾选下载项并去android-sdk-windows\temp文件家中查找文件名称例如:android-2.3.1_r02-linux.zip在前面加上链接h ...

  2. response.setContentType()的作用及参数

    package com.java1234.util; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse ...

  3. sql查阅每一月的数据

    因为项目中需要做数据报表的功能,需要统计每个月的销售额.我找到下面的sql语句.后来经过自己的测试,发现第二句才是可以用的, //String sql="SELECT year(buydat ...

  4. 错误:Duplicate interface definition for class

    错误: Duplicate interface definition for class  "类名称" 原因:在工程中有相同的文件或相同的 @interface 类名称 解决办法: ...

  5. Python md5 sha1 的使用

    版本: Python 2.7 说明: Python 内置的 hashlib 模块中有 md5 和 sha1 加密方法,可以直接使用. md5加密 import hashlib data = 'This ...

  6. Android学习----AndroidManifest.xml文件解析

    一个Android应用程序的结构: 一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了pack ...

  7. python中函数的默认参数陷阱问题

    其实也不能说是陷阱,只是一个不容易注意到的地方,尤其是有其他java/c++类编程语言经验的人员,这里涉及到python的一个特点,所以笔者说是陷阱只是一个噱头而已. def test(item, b ...

  8. jquery获取浏览器的高度和宽度

    <script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...

  9. C语言笔记(结构体与offsetof、container_of之前的关系)

    关于结构体学习,需要了解:结构体的定义和使用.内存对齐.结构体指针.得到结构体元素的偏移量(offsetof宏实现) 一.复习结构体的基本定义和使用 typedef struct mystruct { ...

  10. SQL SERVER 数据库邮件配置

    1.简单了解数据库邮件的概念和使用的传输协议及系统体系: 数据库邮件是从 SQL Server 数据库引擎中发送电子邮件的企业解决方案.通过使用数据库邮件,数据库应用程序可以向用户发送电子邮件.邮件中 ...