Given n non-negative integers a1, a2, ..., a, 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 and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49 题目给出[a1,a2...ai],要求找出一个ai,aj是的min(ai,aj)*(j-i)最大。
有两种方法:
(1)暴力法
两层循环从左到右遍历,算出每一组的面积大小
public class Solution {
public int maxArea(int[] height) {
int maxarea = 0;
for (int i = 0; i < height.length; i++)
for (int j = i + 1; j < height.length; j++)
maxarea = Math.max(maxarea, Math.min(height[i], height[j]) * (j - i));
return maxarea;
}
}

两层循环,复杂度自然是O(n^2)

(2)双指针法

现在假设[a1,a2......an],现在我们算出s=min(a1,an)*(n-1),1和n是长度的边界,剩下的任意高度组合的长度都不能比n-1大,长度如果变成了n-2,

之前算出的面积s如果不想变小,min(ai,aj)就必须比min(a1,an)大。总结一下就是,随着长度的缩小,面积要想变大就必须扩大高度,所以比原先高度低的我们就

不需要再考虑了。

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

一次遍历就能解决,时间复杂度O(n)

[LeetCode]11. Container With Most Water 盛最多水的容器的更多相关文章

  1. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  2. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  3. 011 Container With Most Water 盛最多水的容器

    给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们 ...

  4. Leetcode11.Container With Most Water盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  5. [LeetCode] Container With Most Water 装最多水的容器

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

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

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

  7. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  8. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  9. LeetCode 11. [👁] Container With Most Water & two pointers

    盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

随机推荐

  1. CSS3 -- FlexBox(弹性盒子)

    盒子模型 CSS中有一种基础设计模式叫盒模型,盒模型定义了Web页面中的元素如何来解析. 在盒模型中主要包括width.height.border.background.padding和margin这 ...

  2. [SinGuLaRiTy] 二分图&匈牙利算法

    [SinGuLaRiTY-1019] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 二分图 二分图是图论中一种特殊的图形.顾名思义,二分图G ...

  3. jzoj5683. 【GDSOI2018模拟4.22】Prime (Min_25筛+拉格朗日插值+主席树)

    题面 \(n\leq 10^{12},k\leq 100\) 题解 一眼就是一个\(Min\_25\)筛+拉格朗日插值优化,然而打完之后交上去发现只有\(60\)分 神\(tm\)还要用主席树优化-- ...

  4. Shell学习日记

    if语句的使用 if语句的的格式: if [ expression ] expression 和方括号([ ])之间必须有空格,否则会有语法错误. then statments fi 或者: if [ ...

  5. python 字符串,bytes和hex字符串之间的相互转换

    import binascii datastr='13'#string 类型转换为bytedataByte=str.encode(datastr)#byte串 转换为16进制 byte串 ,比如 b' ...

  6. 贪心+DP【洛谷P4823】 [TJOI2013]拯救小矮人

    P4823 [TJOI2013]拯救小矮人 题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以 ...

  7. STL用法

    map.find(key) 获取map容器中指定键值x的元素,如果找到,返回此元素的迭代器,否则返回map::end()的迭代器(即查找到容器的末尾都没有找到此元素).

  8. 19.Longest Substring Without Repeating Characters(长度最长的不重复子串)

    Level:   Medium 题目描述: Given a string, find the length of the longest substring without repeating cha ...

  9. kali2017.2之***ss安装与使用

    一.命令行安装:apt-get install python-pip    ###安装pipsudo pip install shadowsocks    ###安装ssgedit /etc/shad ...

  10. Mybatis学习笔记(六) —— 动态sql

    通过mybatis提供的各种标签方法实现动态拼接sql. 需求:根据性别和名字查询用户 查询sql: SELECT id, username, birthday, sex, address FROM ...