题目:

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的更多相关文章

  1. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

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

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

  3. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  5. 【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). ...

  6. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  7. LeetCode 11. Container With Most Water (装最多水的容器)

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

  8. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  9. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

随机推荐

  1. [php扩展] php安装扩展注意事项

    添加扩展的时候注意此3项 用的编译器版本:VC11... 安装的php版本:x86/x64 是否线程安全:enabled / disabled

  2. leetcode-3-basic-divide and conquer

    解题思路: 因为这个矩阵是有序的,所以从右上角开始查找.这样的话,如果target比matrix[row][col]小,那么就向左查找:如果比它大,就 向下查找.如果相等就找到了,如果碰到边界,就说明 ...

  3. eclipse去除js(JavaScript)验证错误

    第一步: 去除eclipse的JS验证: 将windows->preference->Java Script->Validator->Errors/Warnings-> ...

  4. ASP.NET MVC 通用角色权限管理系统

    RightControl 介绍 .NET 通用后台角色权限管理系统,已完成.项目地址:http://106.14.77.184/Admin/Login 码云地址:https://gitee.com/L ...

  5. Java-列出一个类实现的所有接口

    package com.tj; public class MyClass2 { public static void main(String[] args) { Class cls = java.la ...

  6. svg path 动画效果

    http://www.zhangxinxu.com/wordpress/2014/04/animateion-line-drawing-svg-path-%E5%8A%A8%E7%94%BB-%E8% ...

  7. appium之toast处理

    注意 toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量. toast定位 1.先看下toast长什么样,如下图,像这种 ...

  8. Java包(package)详解

    java包的作用是为了区别类名的命名空间 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用.. 2.如同文件夹一样,包也采用了树形目录的存储方式.同一个包中的类名字是不同的,不同的包 ...

  9. PHP cannoy modify header information - headers already sent by ....

    我采用的是MVC模式的写法,代码和html分离的写法 <?php require '../mysql_connect.php'; require('../model/functions.php' ...

  10. POJ-1200 Crazy Search,人生第一道hash题!

                                                        Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...