LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/
题目要求:给定n个非负整数a1,a2,...,an ,每一个整数对应一个坐标(i,ai )。以(i,0)和(i,ai )为端点画一条线段,现在选择两条线段,以x轴为底构成一个容器,请问选择那两条线段时可以使容器盛更多的水?
注意:“短板效应”,容量取决于短的那条边,以及两条线段之间的距离
思路:求出Max[min(line1,line2)*(i1-i2)]
方法一:暴力解决,去任意两条不同线段构成容器,求出最大值。时间复杂度O(n2),空间复杂度O(1)。
方法二:两端逼近,从数组的两端开始,不断向中间逼近。时间复杂度O(n),空间复杂度O(1)
package com.huiAlex;
import java.util.Random;
public class ContainerWithMostWater11 {
public static void main(String[] args) {
int[] height = new int[10];
Random ran = new Random();
for (int i = 0; i < height.length; i++) {
height[i] = ran.nextInt(10);
System.out.println(height[i]);
}
ContainerWithMostWater11 cw = new ContainerWithMostWater11();
int a = cw.maxArea(height);
int b = cw.maxArea2(height);
System.out.println("area: " + a);
System.out.println("area2:" + b);
}
// 两端逼近
public int maxArea(int[] height) {
int maxarea = 0;
int l = 0;
int r = height.length - 1;
while (1 < r) {
maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
if (height[l] < height[r])
l++;
else
r--;
}
return maxarea;
}
// 暴力解决
public int maxArea2(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;
}
}
LeetCode:11. ContainerWithWater(Medium)的更多相关文章
- LeetCode:18. 4Sum(Medium)
1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
随机推荐
- 一个查看UI5控件所有公有方法的小技巧
一个很小的tip:比如我想把UI5表格控件里的每列设置成宽度根据显示的内容自适应,需要知道应该调用控件的哪个方法来实现. 一种办法当然是查SAP帮助文档,得知需要调用控件的公有方法setAutoSiz ...
- OC 结构体
void test() { // 这个机构只能在函数内部使用 // 定义一个名为Student的结构体类型 struct Student { int age; // 年龄 char *name; // ...
- nautilus命令
nautilus 是图形程式效果是以当前用户打开图形界面所以如果想以root打开图形界面使用时记得先切为root,sudo没有用的
- 推箱子Unity
逻辑首先有控制台写了一下. 需要注意的地方不少: 进一步,需要考虑3个层面的问题. 前面的位置是空地,成功点,墙,还是箱子. 前面是箱子的时候,箱子的前面是空地,墙,成功点,还是箱子. 当移动的时候, ...
- 【[ZJOI2010]网络扩容】
题目 第一问直接板子敲上去 第二问并不明白直接在残量网络上加边的神仙做法 非常显然我们需要让流量加\(k\),那么我们就使得网络里的总流量为\(maxf+k\),\(maxf\)是第一问求出来的最大流 ...
- CF498D Traffic Jams in the Land
嘟嘟嘟 题面:有n条公路一次连接着n + 1个城市,每一条公路有一个堵塞时刻a[i],如果当前时间能被a[i]整除,那么通过这条公路需要2分钟:否则需要1分钟. 现给出n条公路的a[i],以及m次操作 ...
- python-函数的使用
一.函数的定义 首先,我们来看一个简单的例子来定义函数: def test(): print('hello') 在其中 def : 关键字,用来告诉解释器,接下来的一段代码是一个函数 test : ...
- js 注册控件的onclick事件
今天做了一个全选功能:1.点击全选,全部选中.选中状态再点击全选,全部取消选中2.点击成员,判断是否成员全部选中,true:全选为选中状态,false:全选为未选中状态. 使用js是比较麻烦的就是如何 ...
- 【luogu P1514 引水入城】 题解
题目链接:https://www.luogu.org/problemnew/show/P1514 // luogu-judger-enable-o2 #include <iostream> ...
- ubuntu开启ssh连接
1.安装openssh-server sudo apt-get install -y openssh-server 2.修改/etc/ssh/sshd-config配置 PermitRootLogin ...