前言

难怪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. My.Ioc 代码示例——注册项的注销和更新

    当您需要从 Ioc 容器中注销/删除一个注册项的时候,您会怎么做呢? 有人曾经在 stackoverflow 上提问“如何从 Unity 中注销一个注册项”.对于这个问题,有人的回答是“有趣.你为什么 ...

  2. 佛祖保佑 永无BUG(网转 by atkfc)

        //                   _ooOoo_    //                  o8888888o    //                  88" . ...

  3. 分页技术之PageDataSource类

    之前给大家介绍了分页技术之Gridview控件,今天给大家介绍另外一种分页技术,采用PageDataSource类 + Repeater控件来实现. 前台只需要拖出一个Repeater控件来绑定要显示 ...

  4. Linux CentOS6.4下Mysql数据库的安装与配置

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  5. chrome浏览器调试

    我们在开发前端代码的时候经常会遇到要调试css,js代码的时候,以前在调试的时候可能需要修改了css代码,然后在前台看一下样式,js也是一样的,可能会用alert或者是console.log输出,然后 ...

  6. VueJs一些资料网站链接

    https://github.com/liukaijv/laravel-vue-cmshttps://github.com/allan2coder/VUE2-SPA-Tutorialhttps://g ...

  7. 点击Textbox 给日历控件赋值

    1.前台代码: <asp:TextBox ID="TextBox1" runat="server" onfocus="return show() ...

  8. PYTHON WEATHER

    小玩一下python强大的库文件,调api获取天气情况 #coding:utf-8 import urllib import json content = urllib.urlopen('http:/ ...

  9. 转:nginx防DDOS攻击的简单配置

    文章来自于: 近期由于工作需要,做了些防DDOS攻击的研究,发现nginx本身就有这方面的模块ngx_http_limit_req_module和ngx_http_limit_conn_module. ...

  10. CentOS 6用snmp配合MRTG显示系统状态

    1. 软件安装以yum的方式安装snmp/MRTG 安装SNMP(要记得安装net-snmp-utils,不然snmpwalk将不能使用)yum -y install net-snmp* 安装MRTG ...