题目大意是提供n个非负整数,a1,...,an,可以将其理解为多个边界(垂直的线段),其中ai处于x坐标i处,并且ai代表的线段高度即为ai的值。要求我们取这样的i与j,使得ai与aj以及x坐标轴围成的碗状物能容纳面积最多的水(即abs(i-j)*min(ai,aj)最大)。


最简单的方案就是暴力枚举:

res= -1, maxl = 0, maxr = 0

for(i=0; i < n; i++)

  for(j = i + 1; j < n; j++)

    res = max(abs(i-j)*min(a[i],a[j]), res)

说一下我的思路。假设函数maxCapacity({ai,...,aj})用于计算ai,...,aj中所能围成的碗状物的最大的保存水的容量,而函数capacity(ai,aj)=abs(i-j)*min(ai,aj)。

对于ai,...,aj,其中1<=i<j<=n:

若ai<aj,那么maxCapacity({ai,...,aj})=max(capacity(ai,aj), maxCapacity({ai+1,...,aj})

若ai>aj,那么maxCapacity({ai,...,aj})=max(capacity(ai,aj), maxCapacity({ai,...,aj-1})

若ai==aj,那么maxCapacity({ai,...,aj})=max(capacity(ai,aj), maxCapacity({ai+1,...,aj-1})

说明一下原因,如果ai和aj围成的碗状物的容量不是最大的,那么结果的边界必定不可能包含ai和aj中的较小值。这是因为对于最终的最优边界ab,ae(e>b),不妨假设ab=min(ai,aj),那么(e-b)*min(ab,ae)<=(e-b)*min(ai,aj)<(j-i)*min(ai,aj)=capacity(ai,aj)。这与ab和ae作为最优边界的定义相悖,因此结果的边界必定不可能包含ai和aj中的较小值。对于ai==aj的情况,ai和aj都可以作为较小值,因此ai和aj都不可能是最优边界之一。故最终的maxCapacity可以这样实现:

maxCapacity(a, i, j):

  if(i >= j)

    return 0

  if(ai < aj)

    sub = maxCapacity(a, i+1, j)

  else if(ai > aj)

    sub = maxCapacity(a, i, j-1)

  else

    sub = maxCapacity(a, i+1, j-1)

  return max((j - i) * min(a[i], a[j]), sub)

整段代码不算上的递归的部分的时间复杂度为O(1),而内部只发生一次递归且每次递归都必定将传入集合的大小减少1,而集合的初始大小为n,因此递归最多会发生n次,故总的时间复杂度为O(1)*n=O(n)。


最后老规矩,贴代码

 package cn.dalt.leetcode;

 /**
  * Created by dalt on 2017/6/17.
  */
 public class ContainerWithMostWater {
     public int maxArea(int[] height) {
         return maxArea(height, 0, height.length - 1);
     }

     private int maxArea(int[] height, int i, int j) {
         int sub;
         if (j <= i) {
             return 0;
         } else if (height[i] < height[j]) {
             sub = maxArea(height, i + 1, j);
         } else if (height[i] > height[j]) {
             sub = maxArea(height, i, j - 1);
         } else {
             sub = maxArea(height, i + 1, j - 1);
         }
         return Math.max((j - i) * Math.min(height[i], height[j]), sub);
     }
 }

Leetcode:Container With Most Water分析和实现的更多相关文章

  1. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  2. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  3. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  4. [LeetCode]Container With Most Water, 解题报告

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

  5. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  6. leetcode Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. [Leetcode] Container With Most Water ( C++)

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  8. LeetCode——Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  9. LeetCode Container With Most Water (Two Pointers)

    题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

随机推荐

  1. FFmpeg再学习 -- 视音频基础知识

    最近一直在看雷霄骅 FFmpeg 系列视频,然后将自己的理解总结一下. 参看:<基于 FFmpeg + SDL 的视频播放器的制作>课程的视频 一.视频播放器原理 自己理解: 比如一个 M ...

  2. 深度学习(六十五)移动端网络MobileNets

  3. Python itertools模块中的product函数

    product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...

  4. cursor光标类型

    今天早上在网上看到一篇关于光标类型的总结代码,很好,特定拿来: 最终结果: 代码: <!DOCTYPE html> <html lang="zh-cn"> ...

  5. Linux部分常用命令学习(一)

    什么是linux命令? 是一个可执行程序,就像我们所看到的位于目录/usr/bin 中的文件一样. 属于这一类的程序,可以编译成二进制文件,诸如用 C 和 C++语言写成的程序, 也可以是由脚本语言写 ...

  6. tab显示不同数据

    效果 核心代码 [js] [#escape x as (x)!?html]<!doctype html><html lang="zh-CN"><hea ...

  7. MyEclipse2014安装操作步骤+破解

    第一步 第二步 第三步 第四步 第五步 第六步 破解操作步骤 1.安装完成 MyEclipse2014(适用于 2013 等版本)后,不要打开软件.解压破解文件压缩包,得到一下文件列表:双击 run. ...

  8. fn project 生产环境使用

    此为官方的参考说明   Running Fn in Production The QuickStart guide is intended to quickly get started and kic ...

  9. ORACLE 11G负载均衡测试

    Oracle 11g R2中,引入了SCAN(Single Client Access Name)的特性.该特性的好处在于,在数据库与客户端之间,添加了一层虚拟的服务层,就是所谓的SCAN IP以及S ...

  10. 【转】Jmeter之GUI运行原理

    一.一语道破jmeter 大家都知道我们在应用jmeter的图形化界面来进行操作,保存后生成的是一个.jmx文件. 那么这个.jmx文件中都是些什么呢. <?xml version=" ...