[LeetCode]11. Container With Most Water 盛最多水的容器
Given n non-negative integers a1, a2, ..., an , 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 盛最多水的容器的更多相关文章
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 011 Container With Most Water 盛最多水的容器
给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们 ...
- Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode:盛最多水的容器【11】
LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- LeetCode 11. [👁] Container With Most Water & two pointers
盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
随机推荐
- Vue.js 的几点总结Watchers/router key/render
Vue.js 的几点总结,下面就是实战案例,一起来看一下. 第一招:化繁为简的Watchers 场景还原: 1 2 3 4 5 6 7 8 created(){ this.fetchPostLis ...
- [SinGuLaRiTy] NOIP膜你赛-Day 1
[SinGuLRiTy-1022] Copyright (c) SinGuLaRITy 2017. All Rights Reserved. 对于所有题目:Time Limit:1s || Memo ...
- [Oracle入门级]知识概况
oracle各个版本间的主要技术更新 oracle 增加数据库创建和存储对象 oracle 8i 整体性能提升 oracle9i 实施应用集群 oracle 10g 支持网格计算 oracle 11g ...
- sql开发技巧总结-2
---恢复内容开始--- 1.如何进行行列转换 需求: 列转换成行 select a.`user_name`,sum(b.kills) from user1 a join user_kills b o ...
- P1979 华容道
题意:$n*m$棋盘上$n*m-1$颗棋子,有且只有一个格子为空白格子,每个棋子大小$1*1$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...
- day_04 列表
1. 列表list 能装对象的对象,有序的(按照我们存放的顺序) 以[]表示,里面可以存放大量各种元素,各个元组用逗号隔开 列表也具有索引和切片 2. 列表的增改删查 1. 增 1.append() ...
- day21 pickle json shelve configpaser 模块
1. 序列化:我们在网络传输的时候,需要我们对对象进行处理,把对象处理成方便存储和传输的格式,这个过程就叫序列化 序列化的方法不一定一样,三十目的都是为了方便储存和传输. 在python中有三种序列化 ...
- vue(1)安装
1.安装node.js(https://nodejs.org/en/),我安装的是 v10.15.1 1).在nodejs安装路径下,新建node_global和node_cache两个文件夹 2). ...
- thinkPHP5.0获取器获取原始数据
如果你定义了获取器的情况下,希望获取数据表中的原始数据,可以使用:$cate = Cate::get(1);// 通过获取器获取字段echo $cate->type;// 获取原始字段数据ech ...
- 【ACM】Knapsack without repetition - 01背包问题
无界背包中的状态及状态方程已经不适用于01背包问题,那么我们来比较这两个问题的不同之处,无界背包问题中同一物品可以使用多次,而01背包问题中一个背包仅可使用一次,区别就在这里.我们将 K(ω)改为 K ...