问题:

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.

官方难度:

Medium

翻译:

给定n个非负整数a1,a2,...,an,每一个数值代表坐标轴上的坐标(i,ai)。

画上n条垂直于横坐标的竖线,用于连接点(i,ai)和(i,0)。找到两条线,与x轴一起形成一个容器,能够容纳最多的水。

注意容器不能倾斜。

方法一:

  1. 利用一个二次循环,同时维护一个最大面积max。

方法一的解题代码:

     private static int method_1(int[] height) {
int max = 0;
for (int i = 0; i < height.length - 1; i++) {
for (int j = i + 1; j < height.length; j++) {
int area = Math.min(height[i], height[j]) * (j - i);
if (max < area) {
max = area;
}
}
}
return max;
}

method_1

方法二:

  1. 显然第一种方法的效率是极低的。
  2. 我们注意到,虽然高是由输入决定的,但是底的值却是可控的。有一个思想,维护一个最大高度maxHeight记录遇到的最大高度,在内循环中自后向前遍历,这样一来,底的值始终在减小。这就意味着只需要考虑高的值,就能决定整个容器的面积。所以在遇到小于maxHeight的情况时,可以直接跳过本次循环,不需要再次计算面积。

方法二的解题代码:

     private static int method_2(int[] height) {
int max = 0;
for (int i = 0; i < height.length; i++) {
int maxHeight = 0;
// 内循环反向遍历
for (int j = height.length - 1; j > i; j--) {
int h = Math.min(height[i], height[j]);
// 因为底越来越小,所以只有高度大于最高高度,才有比较面积的意义
if (h > maxHeight) {
// 不考虑面积比较结果,高先赋值
maxHeight = h;
if (h * (j - i) > max) {
max = h * (j - i);
}
}
}
}
return max;
}

method_2

方法三:

  1. 方法二仍然是一个时间复杂度为O(n^2)的二次循环,但是还有优化的策略。
  2. 在一次内循环结束之后,进入下一次内循环,如果height[i]小于上一次的长度,可以直接跳过这次循环。这样一来看似二次循环的问题,可以通过从两侧向中间夹逼,转化为一次循环的问题。

方法三的解题代码:

     public static int maxArea(int[] height) {
if (height == null || height.length < 2) {
throw new IllegalArgumentException("Input error");
}
// 初始面积
int left = 0, right = height.length - 1;
int area = (right - left) * Math.min(height[left], height[right]);
// 左右侧开始的最长高度
int leftMax = height[left];
int rightMax = height[right];
// 从两侧向中间夹逼,即底在不断变小
while (left < right) {
if (height[left] < height[right]) {
left++;
// 更小的高没有比较价值
if (height[left] <= leftMax) {
continue;
} else {
leftMax = height[left];
}
area = Math.max(area, (right - left) * Math.min(height[left], height[right]));
} else {
right--;
if (height[right] <= rightMax) {
continue;
} else {
rightMax = height[right];
}
area = Math.max(area, (right - left) * Math.min(height[left], height[right]));
}
}
return area;
}

maxArea

相关链接:

https://leetcode.com/problems/container-with-most-water/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/medium/Q011.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

 

No.011:Container With Most Water的更多相关文章

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

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

  2. LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium

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

  3. LeetCode11:Container With Most Water

    public int MaxArea(int[] height) { ; ; ; while(start<end) { max=Math.Max(max,Math.Min(height[star ...

  4. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  5. LeetCode--No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  6. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. [LintCode] Container With Most Water 装最多水的容器

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

  8. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

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

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

随机推荐

  1. 通过PowerShell发送TCP请求

    很多时候我们需要通过Socket发送特定的TCP请求给服务器的特定端口来实现探测服务器的指定端口所开启的服务.很多语言都有相应的方法实现上述需求,当然,PowerShell也不例外,比如我们要发送一个 ...

  2. QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)

    最近这阵子,接了个私活,封装一个开发包俗称的SDK给客户调用,查阅了很多人家的SDK,绝大部分用VC编写,而且VC6.0居多,估计也是为了兼容大量的XP用户及IE浏览器,XP自带了VC6.0运行库,所 ...

  3. 让C程序更高效的10种方法(转)

    原文:http://blog.jobbole.com/1198/ 代码之美,不仅在于为一个给定问题找到解决方案,而且还在代码的简单性.有效性.紧凑性和效率(内存).代码设计比实际执行更难 .因此,每一 ...

  4. 【Software Clone】2014-IEEE-Towards a Big Data Curated Benchmark of Inter-Project Code Clones

    Abstract 大数据的克隆检测和搜索算法已经作为嵌入在应用中的一部分. 本文推出一个代码检测基准.包含一些已知的真假克隆代码.其中包括600万条真克隆(包含type-1,type-2,type-3 ...

  5. DbUtils使用例子

    DbUtils: JDBC Utility Component Examples This page provides examples that show how DbUtils may be us ...

  6. 用 .NET Reflector 8 查看 System.Windows.Controls 命名空间下的类

    为了学习自定义控件,就想看看WPF基本元素的代码.使用到工具.NET Reflector. System.Windows.Controls 命名空间在PresentationFramework.dll ...

  7. 怎样设置一个DIV在所有层的最上层,最上层DIV

    怎样设置一个DIV在所有层的最上层,最上层DIV,其实很简单,只需要在这个DIV上使用这个样式即可,z-index:99999

  8. Tips10:你可以像写文档一样自由的复制粘贴游戏组件(Game Object Component)

    相对于添加组件后再进行调整和赋值,通过复制和粘贴已有游戏组件能够带来更高的效率.

  9. "浅谈Android"第一篇:Android系统简介

    近来,看了一本书,名字叫做<第一行代码>,是CSDN一名博主写的,一本Android入门级的书,比较适合新手.看了书之后,有感而发,想来进行Android开发已经有一年多了,但欠缺系统化的 ...

  10. HT for Web中3D流动效果的实现与应用

    流动效果在3D领域有着广泛的应用场景,如上图中医学领域可通过3D的流动直观的观察人体血液的流动,燃气领域可用于监控管道内流动的液体或气体的流向.流速和温度等指标. 如今企业数据中心机房普遍面临着设备散 ...