描述

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 容器最大水容量的更多相关文章

  1. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

  2. 【LeetCode每天一题】Container With Most Water(容器中最多的水)

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  3. LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  4. [LintCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  5. LeetCode第[11]题(Java):Container With Most Water 标签:Array

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  6. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  7. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  8. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

  9. LeetCode--No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

随机推荐

  1. 126邮箱发送邮件python实现

    126邮箱发送邮件python实现 from email.mime.text import MIMEText from email.utils import formataddr import smt ...

  2. .NET DateTime 源码学习

    今天下载了微软.Net 源码,看了一下DateTime类,做下记录 DaysInMonth 这个方法是获取某年某月的天数,平时直接用觉得很简单,今天看到源码,发现设计的还是很好的 我想如果是我的话,封 ...

  3. Python系列之内置函数

    内置函数 一.数学运算类: abs(a):求绝对值如果参数是个复数则返回复数的模. a = abs(-1) print(a) >>>1 compilex([real[, imag]] ...

  4. HDU1789 Doing Homework again

    基础单调队列: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...

  5. RabbitMQ高可用集群配置

    1.安装RabbitMQ 1)下载和安装erlang 下载erlang wget http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el6.x ...

  6. python 携带cookie访问网站(python接口测试post)

    最近在使用自己研究性能测试工具的时候想到,使用python向服务器不断发送数据以作为并发测试.大概情况如下: #coding=utf-8 import urllib2 import urllib im ...

  7. 移动端效果之IndexList

    写在前面 接着前面的移动端效果讲,这次讲解的的是IndexList的实现原理.效果如下: 代码请看这里:github 移动端效果之swiper 移动端效果之picker 移动端效果之cellSwipe ...

  8. linux 内核 zImage 生成过程分析

    1. 依据arch/arm/kernel/vmlinux.lds 生成linux内核源码根目录下的vmlinux,这个vmlinux属于未压缩,带调试信息.符号表的最初的内核,大小约23MB: arm ...

  9. swiper使用小结

    最近做一个移动端项目想用Swiper移动端插件,需求实现一个轮播图的效果,并且需要自定义分页器,效果跟这个差不多这里demo 好吧,开始动手! 注意参考的3.0Swiper的API文档需要引入3.0版 ...

  10. TsBatis 预览

    前言 在发布了 mybatis-dynamic-query之后感觉基本功能稳定了,而且现在在工作项目开发效率大大提高,而且非常易于维护. 最近准备带几个小朋友以前用typescript 打通前后端,当 ...