[LeetCode][Java] 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.
题意:
给定n个非负整数a1, a2,
..., an,
每一个代表在坐标(i, ai)上的一点,连接(i, ai)
and (i, 0)形成n条垂直线。找出两条垂线,和x坐标形成一个容器,使得这个容器包括的水最多。
注意:你不能够倾斜容器。
算法分析:
两边夹的策略
两个指标i j往中间走。每次计算i和j之间的面积。假设比眼下最大面积大。则更新最大面积,否则让两者之间较小的数的指标往前走。
假设height[i] <= height[j],那么i++,由于在这里height[i]是瓶颈,j往里移仅仅会降低面积,不会再添加area。
这是一个贪心的策略,每次取两边围栏最矮的一个推进,希望获取很多其它的水。
AC代码:
public int maxArea(int[] height)
{
int left=0;
int right=height.length-1;
int temwater;
int res=0;
while(left<right)
{
temwater=Math.min(height[left], height[right])*(right-left);
if(temwater>res) res=temwater;
if(height[left]<height[right])
left++;
else
right--;
}
return res;
}
[LeetCode][Java] Container With Most Water的更多相关文章
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 【JAVA、C++】LeetCode 011 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- 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 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
随机推荐
- yum安装php7.2
文章来源:https://www.cnblogs.com/hello-tl/p/9404655.html 分享一个算是比较完美的php7.2yum安装 0.更换yum原 # yum install e ...
- Python面向对象(成员)(二)
1. 成员 在类中你能写的所有内容都是类的成员 2. 变量 1. 实例变量: 由对象去访问的变量. class Person: def __init__(self, name, id, gender, ...
- web开发框架Flask学习一
flask框架 用Python做Web开发的三大框架特点 Django 主要特点是大而全,集成了很多的组件,例如:Admin Form Model等,不管你用不用的到,他都会为 你提供,通常用于大型W ...
- spring-cloud-sleuth 学习资源
https://www.baeldung.com/spring-cloud-sleuth-single-application https://howtodoinjava.com/spring-clo ...
- 《C#高级编程》笔记系列第三弹
我们在开发WinForm时,经常会看到partial关键字,比如,我们新建一个Windows Form时,后台代码自动添加如下: 1 public partial class Form1 : Form ...
- appium+python自动化-adb logcat查看日志
前言 做app测试,遇到异常情况,查看日志是必不可少的,日志如何输出到手机sdcard和电脑的目录呢?这就需要用logcat输出日志了 以下操作是基于windows平台的操作:adb logcat | ...
- vim 第三章 插入模式
vim 第三章 插入模式 在普通模式下可以删除 复制 及粘贴的命令 在插入模式下也存在以中方便快捷的方式 能够粘贴寄存器中文本 两种方式来插入键盘上不存在的非常用字符 替换模式 ...
- pat 1029 1029. 旧键盘(20)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在2行中分别给出应该输入的文字.以及实际 ...
- lvs+keepalive主从和主主架构
下面配置主从 1)关闭SELinux和防火墙 vi /etc/sysconfig/selinux SELINUX=disabled setenforce 临时关闭SELinux,文件配置后,重启生效 ...
- [luoguP1131] [ZJOI2007]时态同步(贪心)
传送门 显然是一棵树. 又显然一段一段地增加比较优. 我们可以dfs,并且尽量把每一个节点所有子树中所有节点的时间增加到一样. #include <vector> #include < ...