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 ...
随机推荐
- SQL server学习(三)T-SQL编程、逻辑控制语句和安全模式
T-SQL编程 T-SQL编程与C语言类似,只是语法稍有不同而已,总体思想还是没有变化的.多的就不说了,还是从变量开始. 变量也分为全局变量和局部变量,表示方式稍有不同. 局部变量: 局部变量必须以标 ...
- MongoDB数据库的安装、配置和使用
1.下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.7.tgz 2.解压安装包 tar -zxf mo ...
- 数据库表反向生成(二) Django ORM inspectdb
在前一篇我们说了,mybatis-generator反向生成代码. 这里我们开始说如何在django中反向生成mysql model代码. 我们在展示django ORM反向生成之前,我们先说一下怎么 ...
- ovs2.7 在系统重启后,再次使用时提示数据库无法连接的问题。
问题现象如下,ovs开始安装后,对ovs的操作是正常的,但是,现在系统重启后,OVS的操作第一条命令就失败,如下: 问题解决方法: 参考 http://blog.csdn.net/xyq54/art ...
- Ricequant-米筐金工-估值因子
Ricequant米筐金工--因子分析 作者:戴宇.小湖 上一篇介绍了单因子检验是因子分析前重要的一个步骤,是构建因子库.建立因子模型的基础,这篇报告首先对常见估值因子进行初步的检验. 第一篇.估值因 ...
- Linux入门(16)——Ubuntu16.04下配置sublime text 3使用markdown
sublime text 3安装两个插件: MarkDown Editing OmniMarkupPreviewer 有的人使用 MarkDown Editing markdownpreviewer ...
- svn的简介
Svn(Subversion)是近年来崛起的版本管理工具,在当前的开源项目里(J2EE),几乎95%以上的项目都用到了SVN.Subversion项目的初衷是为了替换当年开源社区最为流行的版本控制软件 ...
- python 模块的概念介绍
模块 模块:本质就是一个.py文件分为三部分:内置模块.第三方模块,自定义模块 模块: 顶层文件 python模块python模块可以将代码量较大的程序分割成多个有组织的.彼此独立但又能互相交互的代码 ...
- python 文件相关知识
字符编码相关 什么是字符编码 字符编码的类型 字符编码的使用 python2和python里字符编码的区别 文件的相关 文件的基础操作 打开文件的模式 字符编码 什么是字符编码在计算机里只识别二进制, ...
- asp.net中使用jquery ajax保存富文本的问题
前提:为了保证页面的不刷新行为,所以采用了html+jquery+handler的页面保存方式,通过ajax将富文本内容传递给一般处理程序进行操作. 一.问题:1.大文件无法上传? 2.传入handl ...