题目难度:Medium

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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轴形成一个容器,找到使容器包含最多的水的两条,返回水的体积。

注意:不能倾斜容器,n至少是2。

思路:遍历每一种可能的体积,与一个temp值相比较,比temp大则temp等于这个体积

思路一Code:48 / 49 test cases passed. ——  Time Limit Exceeded            时间复杂度O(N2)

     public int maxArea(int[] height) {
int result = 0;
for (int i = 0; i < height.length; i++) {
for (int j = i + 1; j < height.length;j++) {
int area = getMin(height[i], height[j]) * (j - i);
if (area > result) {
result = area;
}
}
}
return result;
}
private int getMin(int a, int b) {
return a>b?b:a;
}

我承认这种方法是蠢了点,但是貌似没毛病啊,怎么会超时?

点开detail一看49的测试用例。。。。

[15000,14999,14998,14997,........[此处省略n万个数字]........,6004,6003,6002,600... 28895 more chars

emmm……

我去你妹妹的吻。。。

带着去瞻仰的心情点开了solution……

我那个区,我咋就没想到!。。。

答案思路:从两边的“木板”向中间同时移动,取两条木板面积,与一个temp值相比较,比temp大则temp等于这个体积

【精髓在于:假设已知一木板X已经比对面的木板Y要长,那么根据短板效应(底面积不变大时,容器盛水容量取决于短板长度),不管怎么继续往前移动(缩小底面积)此X木板,得到的结果也不会比刚才X与Y组合的结果更大。

因此,从两边取两条“木板”时,只需要移动相对短的直线的那一边的指针即可达到寻找最大面积的效果】

Code:49测试用例—9ms(beats 69.61%)     时间复杂度:O(N)

 public static int maxArea2(int[] height) {
int maxarea = 0, l = 0, r = height.length - 1;
while (l < r) {
maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
if (height[l] < height[r])
l++;
else
r--;
}
return maxarea;
}

总结:面对求最大(小)面积、体积这种题,应该要考虑刷选遍历可能结果,即某些相对更小(大)的可能结果已经确定小于(大于)在已经遍历过的结果里了(利用短板效应,和公共边效应等)

   例如本题中,如果进行全遍历,那么在一边木板本来就是比对面高的情况下,不管怎么继续往前移动此木板,得到的结果只会比刚才的结果更小,强行全遍历就造成了遍历冗余。

【补:求两者最大值最小值可使用Math包的方法,其实底层和我写的一样都是三目表达式(以后使用记得加括号,因为这种表达式是靠左优先),不过可以简化代码,增加可读性】

LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium的更多相关文章

  1. LeetCode第[42]题(Java):Trapping Rain Water (数组方块盛水)——HARD

    题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of ...

  2. LeetCode第[11]题(Java):Container With Most Water 标签:Array

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  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第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  5. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  6. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  8. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. [LeetCode每日一题]88. 合并两个有序数组

    [LeetCode每日一题]88. 合并两个有序数组 问题 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 初始化 n ...

随机推荐

  1. Byzantine failures

    https://baike.baidu.com/item/拜占庭将军问题/265656?fr=aladdin 拜占庭将军问题(Byzantine failures),是由莱斯利·兰伯特提出的点对点通信 ...

  2. <2014 05 09> Lucida:我的算法学习之路

    [转载] 我的算法学习之路 关于 严格来说,本文题目应该是我的数据结构和算法学习之路,但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法(例如算法导论指的实际上是数据结构和算法导论),所以 ...

  3. 安装Centos 7操作系统

    一.安装前准备 VMware workstation.CentOS-7-x86_64 系统镜像. 二.开始新建虚拟机 选择典型-下一步  选择稍后安装操作系统-下一步  选择LINUX-CentOS ...

  4. 总结! http post请求 application/x-www-form-urlencoded body体数据获取不到?

    首先,简单介绍下Http请求中Content-Type类型 类型格式:type/subtype(;parameter)? type 主类型,任意的字符串,如text,如果是*号代表所有: subtyp ...

  5. for and range()

    pyhon 中 for 循环可以遍历任何序列的项目,如一个字典或者一个字符. for 循环格式一般如下: for <variable-变量> in <sequence-序列>: ...

  6. 如何做好部门以及公司的文档管理、知识管理以及情报管理?——By Me

    之前针对部门的文档管理开发平台进行过一次需求调研分析,对于实现方案与我们的实际需求之前的满足情况系统梳理了一下,我觉得对于有类似需求的团队或者公司应该有可以借鉴的地方,发到这里供大家参考.如有不正之处 ...

  7. nodejs升级的两种方法

    方法一: 用n升级nodejs # 全局安装n$ npm install -g n# 升级到最新稳定版$ n stable # 升级到最新版$ n latest# 升级到定制版$ n v7.10.0# ...

  8. Red Hat Enterprise Linux

    以下是支持 Docker 的 RHEL 版本: Red Hat Enterprise Linux 7 (64-bit) Red Hat Enterprise Linux 6.5 (64-bit) 或更 ...

  9. python爬取当当网的书籍信息并保存到csv文件

    python爬取当当网的书籍信息并保存到csv文件 依赖的库: requests #用来获取页面内容 BeautifulSoup #opython3不能安装BeautifulSoup,但可以安装Bea ...

  10. 推荐系统第2周--itemCF和userCF

    推荐系统分类 基于应用领域分类:电子商务推荐,社交好友推荐,搜索引擎推荐,信息内容推荐基于设计思想:基于协同过滤的推荐,基于内容的推荐,基于知识的推荐,混合推荐基于使用何种数据:基于用户行为数据的推荐 ...