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.
分析
题目在时间复杂度O(n2)下一个两次循环遍历即可,但需要时间复杂度O(n)的下完成,通过分析,可以采用左右夹逼的方式来做,如果说左边数大于右边的数,则右边向左偏移一位,否则左边向右偏移一位
代码
package com.lilei.myes.es.pack1108;
public class ContainerWithMostWater {
public static void main(String[] args) {
System.out.println(cwmw(new int[]{3,2,3,4,5}));
}
static int cwmw(int[] array){
int area = 0;
int left = 0;
while(array[left] <=0){
left++;
}
int right = array.length-1;
while(array[right] <=0){
right--;
}
while(left < right){
int height = array[left]>array[right]?array[right] : array[left];
area = Math.max(area, height * (right - left));
if (array[left] > array[right])
array[left] = right--;
else
array[left] = left++;
}
return area;
}
}
Container With Most Water 容器最大水容量的更多相关文章
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
- 【LeetCode每天一题】Container With Most Water(容器中最多的水)
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode第[11]题(Java):Container With Most Water 标签:Array
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- 关于Container With Most Water的求解
Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...
- LeetCode--No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
随机推荐
- C# 中函数内定义函数的委托方法
//定义委托方法Action(无返回值)Func(有返回值) //无返回值委托 Action<string> SetKeyAndValue = delegate(string key) { ...
- clone github报Permission denied (publickey) 解决方案
问题描述 问题产生的原因,不是很清楚,就不管了.在执行git clone git@github.com:****.git 的时候报了Permission denied (publickey). War ...
- ubuntu环境下lnmp环境搭建(2)之Nginx
1. ubuntu编译安装nginx http://www.cnblogs.com/zhangjun516/archive/2013/02/03/2890990.html 1. 手动编译安装 Ngin ...
- c++ 11 移动语义、std::move 左值、右值、将亡值、纯右值、右值引用
为什么要用移动语义 先看看下面的代码 // rvalue_reference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #includ ...
- SqlServer和Oracle中一些常用的sql语句8 触发器和事务
--创建和执行事后触发器 --更新仓库备份表中记录时自动创建数据表且插入三条记录 create trigger db_trigger1 on 仓库备份 for update as begin if E ...
- Excel导出插件
前言 一个游戏通常需要10多个Excel表格或者更多来配置,一般会通过导出csv格式读取配置. 本文提供导出Excel直接生成c#文件,对应数据直接生成结构体和数组,方便开发排错和简化重复写每个表格的 ...
- 【装逼利器效率软件】一张图问你想不想用Launchy
简述:Launchy博客园很多文章,长篇大论文字太多. 一张图问你想不想用? 长话多说: 一.设置Launchy扫描目录,安装后会默认,个人推荐自定义目录比较好 二.自行建立快捷方式别名文件夹,存放各 ...
- 转:stringstream的用法
[本文来自]http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/ ...
- JAVA中HashMap和Hashtable区别
Hashtable和HashMap在Java面试中相当容易被问到,甚至成为了集合框架面试题中最常被考的问题,所以在参加任何Java面试之前,都不要忘了准备这一题. 我们先看2个类的定义 public ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...