前言

难怪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. sublime text There are no packages 解决!

    1.问题如下图 解决如下: 1.取得sublime.wbond.net的IPv4地址.在命令提示符中输入以下命令: ping sublime.wbond.net 获得  pv 4 ip     2.C ...

  2. Web服务图片压缩,nginx+lua生成缩略图

    背景 目前而言,用移动端访问Web站点的用户越来越多,图片对流量的消耗是比较大的,之前一个用户用我们网站的app浏览的时候,2个小时耗去了2个G的流量,这是个很严重的问题,需要对图片进行压缩,减少对用 ...

  3. [LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

    class Solution { public: int singleNumber(int A[], int n) { int i,j; ; i<n; i++) { ; j<n; j++) ...

  4. 【USACO 1.1.1】你的飞碟在这儿

    [问题描述] 一个众所周知的事实,在每一慧星后面是一个不明飞行物UFO. 这些不明飞行物时常来收集来自在地球上忠诚的支持者. 不幸地,他们的空间在每次旅行只能带上一群支持者. 他们要做的是用一种聪明的 ...

  5. iPhone 6 图像渲染揭秘(转)

    几天前,Apple发布了iPhone 6 Plus. 新的iPhone大幅改变了图像在屏幕上渲染的方式.我们做了一个图表进行详细分析. 分析. 转自:转送

  6. 第三方分页控件aspnetPager出现问题解决方法

    问题描述: 今天在打开以前的项目使用vs2013打开后并且生成解决方案的时候发现报错了.经过检查发现是由于第三方分页控件aspnetPager在页面上不能引用到了. 解决方法: 1. 首先将AspNe ...

  7. 初学Javascript对象

    <script> var p=new Object(); //属性 p.width=; p.height=; p.num=; p.autotime=; //方法 p.autoplay=fu ...

  8. 关于几种常用的Adapter使用区别

    Adapter常用的实现类如下: 1.ArrayAdapter:简单.易用的Adapter,通常用于将数组或List集合的多个值包装成多个列表项. 2.SimpleAdapter:并不简单.功能强大的 ...

  9. JS之路——数组对象

    String字符串对象 concat() 返回一个由两个数组合并组成的新数组 join() 返回一个由数组中的所有元素连接在一起的String对象 pop() 删除数组中最后一个元素 并返回该值 pu ...

  10. QT 设置SizePolicy的例子(简单明了)

    http://hi.baidu.com/cybertingred/item/e8eadaad0c7f62f615329be7   QPushButton *left = new QPushButton ...