[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 ...
随机推荐
- Beautiful Soup 4.2.0 doc_tag、Name、Attributes、多值属性
找到了bs4的中文文档,对昨天爬虫程序里所涉及的bs4库进行学习.这篇代码涉及到tag.Name.Attributes以及多值属性. ''' 对象的种类 Beautiful Soup将复杂HTML文档 ...
- Linux Shell脚本教程:30分钟玩转Shell脚本编程
http://c.biancheng.net/cpp/shell/ Linux在线体验: http://compileonline.com/ Linux命令查询: http://man.linuxde ...
- SpringData及SpringData JPA的理解和简单应用
SpringData是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得 数据库的访问变得方便快捷,并支持map-reduce框架和云计算数据服务.此外,它还支持 基于关系型数据库的数据 ...
- 误删除innodb ibdata数据文件-之恢复
今天在群里看到有人说不熟悉innodb把ibdata(数据文件)和ib_logfile(事务日志)文件误删除了.不知道怎么解决.当时我也不知道怎么办.后来查阅相关资料.终找到解决方法.其实恢复也挺简单 ...
- linux 搭建apache 服务器
1.查看apache服务器 /etc/init.d/httpd status 若没有,则使用yum -y install httpd 安装软件 2.设置开机启动 chkconfig httpd o ...
- performSelectorOnMainThread
在多线程操作中,有一个著名的错误,叫做"Tried to obtain the web lock from a thread other than the main thread or th ...
- 【UML】概述以及面向对象技术总结
导读:结束了软工文档后,就开始了UML的学习,不管学习什么,都要先从整体上去把握,然后再从细节上去分析理解.在视频的开头,就对UML进行了概述.然后接着讲了面向对象技术,用例图,类图和包图等.看着软工 ...
- 【JavaScript 3—基础知识点】:运算符
导读:其实看到这个运算符的学习,很有一种熟悉感,因为在总体看来,和之前的C++有很多类似的地方,但当时觉得简单,没有总结.所以,这次一定得总结了.其实,知识的罗列,基础的积累,在学习中也很重要. 一. ...
- HDU-4738 Caocao's Bridges,注意重边不是桥!
Caocao's Bridges 题意:曹操赤壁之战后卷土重来,他在n个小岛之间建立了m座桥.现在周瑜只有一颗炮弹,他只能炸毁一座桥使得这些岛屿不再连通.每座桥上都可能会有士兵把手,如果想安放***那 ...
- POJ-1200 Crazy Search,人生第一道hash题!
Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...