2018-07-31 17:28:42

问题描述:

问题求解:

很容易想到的是Brute Force,也就是枚举所有可能的pairs,这种解法的时间复杂度为O(n ^ 2),由于本题的数据规模较大,会TLE。那么就要对算法进行改进了。

这里用到的解法是Two Pointers,左右各设置一个指针,l 和 r。

首先计算最初的面积 curArea = Math.min(height[l], height[r]) * (r - l)。

如果height[l] < height[r],那么可以想见的是将右边线无论如何向左调整,都是无法继续增大面积的,因此此时只能调整左边线,l++。

同理height[l] > height[r],那么只能调整右边线来谋求更好的解,r--。

如果出现了height[l] = height[r],则可任意调整左边或者右边来谋求更好的解。

    public int maxArea(int[] height) {
int res = 0;
int l = 0;
int r = height.length - 1;
while (l < r) {
int curArea = Math.min(height[l], height[r]) * (r - l);
if (curArea > res) res = curArea;
if (height[l] < height[r]) l++;
else r--;
}
return res;
}

最大容积 Container With Most Water的更多相关文章

  1. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  2. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

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

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

  4. 67. Container With Most Water

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

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

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

  6. No.011 Container With Most Water

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

  7. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

  8. [leecode]---11.container with most water

    description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...

  9. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

随机推荐

  1. POJ 1182 并查集

    Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...

  2. linux编程之pipe()函数

    管道是一种把两个进程之间的标准输入和标准输出连接起来的机制,从而提供一种让多个进程间通信的方法,当进程创建管道时,每次 都需要提供两个文件描述符来操作管道.其中一个对管道进行写操作,另一个对管道进行读 ...

  3. python webdriver 从无到有搭建混合驱动自动化测试框架的过程和总结

    一步一步实现混合驱动自动化测试框架的搭建 混合驱动自动化测试框架,是一个非常高级的框架,非常好用,但也很难,不好掌握,需要多练习,就像搭建数据驱动框架一样,需要自己去一点一点的写,一边搭建一边做思路整 ...

  4. java程序初始化顺序

    使用场景:  在java程序中,当实例化对象时,对象的所在类的所有成员变量首先要进行初始化,只有当所有类成员完成初始化后, 才会调用对象所在类的构造函数创建对象. 初始化的原则: (1)静态对象优先于 ...

  5. html 5实用特性之data属性

    HTML 5之前,我们必须依赖于class和rel属性来存储需要在网站中使用的数据片段,这种做法有时会在网站的外观和实用性之间产生冲突.而HTML 5 Data属性的存在就能很好满足需要. HTML5 ...

  6. map set iterator not incrementable 解决办法

    例子: #include <iostream> #include <map> using namespace std; int main() { map<int, int ...

  7. C++类的静态成员变量初始化 Win32 API 定时器使用

    1.类的静态成员变量 .h 类声明入下 class A { public: static int x; }; .cpp文件 这样初始化. ; 2.定时器使用 1.SetTimer(HWND,UINT, ...

  8. python字符串、列表和文件对象总结

    1.字符串是字符序列.字符串文字可以用单引号或者双引号分隔. 2.可以用内置的序列操作来处理字符串和列表:连接(+).重复(*).索引([]),切片([:])和长度(len()).可以用for循环遍历 ...

  9. cmd命令安装、卸载、启动和停止Windows Servic

    1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft.NET ...

  10. Python3基础 file open+write 对不存在的txt进行创建与写入

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...