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. 4、OpenCV Python 像素运算

    __author__ = "WSX" import cv2 as cv import numpy as np #前提需要运算的图像大小和类型完全相同 #算术运算 加减乘除 #逻辑运 ...

  2. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

  3. k8s安装

    docker pull mirrorgooglecontainers/kube-scheduler:v1.13.3 安装docker 使用阿里的源https://mirrors.aliyun.com/ ...

  4. Linux系统之ssh命令

    ssh命令用于远程登录上Linux主机. 常用格式:ssh [-l login_name] [-p port] [user@]hostname更详细的可以用ssh -h查看. 不指定用户: ssh 1 ...

  5. Redis学习笔记(5)—— Redis的持久化方案&Redis的集群搭建

    一.Redis的持久化方案 Redis的高性能是由于其将所有数据都存储在了内存中,为了使Redis在重启之后仍能保证数据不丢失,需要将数据从内存中同步到硬盘中,这一过程就是持久化. Redis支持两种 ...

  6. 爬取猫眼电影top100的代码

    废话不说,代码附上: #encoding:utf-8 import requests import re import json from multiprocessing import Pool #多 ...

  7. Ansible自动化运维工具

    ansible软件介绍 python语言是运维人员必会的语言!  ansible是一个基于Python开发的自动化运维工具!(saltstack)  其功能实现基于SSH远程连接服务!  ans ...

  8. Clover笔记

    1. Clover传参 a. jobflow传到子graph(GraphParameters): 设值: 用全局GraphParameters传递,在ExecuteGraph中的output mapp ...

  9. eclipse安装阿里规范模板

    https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化

  10. C++: int int& int * int**的区别、联系和用途

    1.int; int是C++关键字,表示整型,其大小是32位有符号整型,表示的范围是-2,147,483,648 到 2,147,483,647:在声明和定义变量时使用,它表示的意思是所声明或所定义的 ...