最大容积 Container With Most Water
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的更多相关文章
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 67. Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- 关于Container With Most Water的求解
Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...
- [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: 找出数组中最大的数 ...
- Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...
随机推荐
- SQL Server查询中特殊字符的处理方法
SQL Server查询中,经常会遇到一些特殊字符,比如单引号“'”等,这些字符的处理方法,是SQL Server用户都应该需要知道的. 我们都知道SQL Server查询过程中,单引号“'”是特殊字 ...
- mongodb部署
windows版本 http://dl.mongodb.org/dl/win32/x86_64 安装教程 https://docs.mongodb.org/manual/tutorial/instal ...
- mac下安装了brew
使用mac后发现很多软件都可以通过终端命令brew...来安装. 查了一下,发现brew原来是osx系统上的软件包管理工具,全名是Homebrew,官网:https://brew.sh(这官竟然还包含 ...
- 本地缓存之GUAVA
项目开发中,很多配置数据需要缓存,一般来说,开发人员都会手动写HashMap,HashSet或者ConcurrentHashMap,ConcurrentHashSet缓存数据,但是这样的缓存往往存在内 ...
- Python tricks(6) -- python代码执行的效率
python作为一个动态语言, 本身学习曲线比较平滑, 效率相比起来会比c++和java低一些. 脚本语言都是运行时编译的, 这个对于效率的影响是非常大的. 我借用参考1的代码, 加了点代码impor ...
- linux基础命令---umask
umask 指定创建文件时所需要的权限掩码,掩码的执行权限对于文件没有效果.如果模式以数字开头,则解释为八进制数字:否则解释为符号模式掩码,类似于chmod(1)所接受的模式掩码.如果省略模式,则打印 ...
- ubuntu14.04无法安装Curl,需要先升级sudo apt-get update
ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...
- python之路----模块调用
如何使用模块? 1 import 示例文件:自定义模块my_module.py,文件名my_module.py,模块名my_module #my_module.py print('from the m ...
- 火狐使用Ctrl新开窗口不生效
使用window.open新开页面,火狐浏览器无法使用Ctrl新开窗口后页面停留在当前页面,兼容性问题,使用<a>或者<router-link>标签即可解决 --贡献者:毛毛
- ELK学习笔记之Logstash详解
0x00 Logstash概述 官方介绍:Logstash is an open source data collection engine with real-time pipelining cap ...