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. LeetCode-MinimumDepthOfBinaryTree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  2. SQL Server报“GUID应包含带4个短划线的32位数”

    转自:http://www.seayee.net/article/info_106.html 最近在配置一台服务器的MS SQL Server 2005的维护计划自动备份数据库,能创建维护计划,但设置 ...

  3. FastDFS+nginx+keepalived集群搭建

    安装环境 nginx-1.6.2 libfastcommon-master.zip FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastd ...

  4. pandas练习(二)------ 数据过滤与排序

    数据过滤与排序------探索2012欧洲杯数据 相关数据见(github) 步骤1 - 导入pandas库 import pandas as pd 步骤2 - 数据集 path2 = ". ...

  5. 如何制作Windows镜像

    1.在https://msdn.itellyou.cn/网站中下载(使用迅雷)Windows2003R2 中文版ISO 2.使用qemu-img create命令创建一个空的 后缀为.img的文件 q ...

  6. Oracle查询session连接数和inactive以及 概要文件IDLE_TIME限制用户最大空闲连接时间

    -----############oracle会话和进程################----------------查询会话总数select count(*) from v$session;--查 ...

  7. python构造栈结构

    栈:是一种先进后出的数据结构:本片文章,我们用python的面向对象来构造这样的数据结构. 栈中的每一个数据除了存储当前的数值外,还存储着当前数值下一个数据的类型(注意不是下一个数据的数值). cla ...

  8. PHP-ThinkPHP5砍价活动相关设计

    近期我们公司项目里陆陆续续有很多为了招引新用户的活动推出,砍价的活动由我来负责,我们的项目是在微信浏览器里供用户浏览访问. 大概描述:进入砍价活动列表页选择有意向的商品,用户点击商品图片可以看到WEB ...

  9. xargs 原理&使用

    1. 简介 之所以能用到这个命令,是由于很多 linux 命令不支持用管道传递参数,例如 find /sbin -perm +700 | ls -l 这个命令是错误的 find /sbin -perm ...

  10. 20145301 赵嘉鑫 《网络对抗》Exp5 MSF基础应用

    20145301 赵嘉鑫 <网络对抗>Exp5 MSF基础应用 一 实验链接 渗透实验一:MS08_067渗透实验 渗透实验二:MS14_064渗透实验  (首用) 渗透实验三:Adobe ...