最大容积 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 ...
随机推荐
- 企业中如何批量更改mysql中表的存储引擎?
一.首先必须熟悉Mysql中有哪些基本的数据库,在mysql中database等价于schema,默认的基本库有四个:mysql,information_schema,performance_sche ...
- uva11383 转化为 二分图匹配
给定一个n*n矩阵,每个格子里都有一个正整数w(i,j).你的任务是给每行确定一个整数row(i),没列也确定一个正整数col(i),使得对于任意格子(i,j),w(i,j) <= row(i) ...
- Groovy中的闭包
Closures(闭包) 本节主要讲groovy中的一个核心语法:closurs,也叫闭包.闭包在groovy中是一个处于代码上下文中的开放的,匿名代码块.它可以访问到其外部的变量或方法. 1. 句法 ...
- Python中*args和**kwargs的区别
(注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 中秋的夜,微凉,但却始终看不见月亮. 我想,它一定是害羞了,悄悄的躲到了乌云的后面. 嗯,就是这样,我真是太TM机智了. 正 ...
- 每天一个Linux命令(1)ls命令
ls是list的缩写,ls命令是Linux系统下最常用的命令之一. ls命令用于打印当前目录的清单,如果指定其它目录,那么就会显示其他目录的文件及文件夹的清单. 通过ls 命令还可以查看文件其它的详细 ...
- Linux基础命令---cut
cut 将文件中每一行的指定内容显示到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 cut [ ...
- Oracle查询session连接数和inactive以及 概要文件IDLE_TIME限制用户最大空闲连接时间
-----############oracle会话和进程################----------------查询会话总数select count(*) from v$session;--查 ...
- LOJ10067 构造完全图
LOJ10067 构造完全图 最小生成树 每次找到最小的边,将边两端的块合并 (我之前想的是什么鬼) #include<cstdio> #include<algorithm> ...
- Observer模式实践
Observer 模式在实践中的应用场景: 为 Point 类设计一个数据绑定机制,当其坐标 x 或 y 被更改时,可以通知外界其更改的过程.将更改过程打印在控制台上.考虑使用松耦合设计. 代码: # ...
- 01: MySql简介
MySQL其他篇 目录: 参考网站 1.1 数据库介绍 1.2 视图 1.3 触发器 1.4 事物 1.1 数据库介绍返回顶部 1.什么是数据库? 1. 数据库(Database)是按照数据结构来组织 ...