11. Container With Most Water (Medium)#

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 and n is at least 2.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

    The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

solution##

第一种解法:暴力解法,也是最容易想到的解法

class Solution {
public int maxArea(int[] height) {
int max = 0;
for (int i = 0; i < height.length; i++)
{
for (int j = i + 1; j <height.length; j++)
{
int s = 0;
if (height[i] > height[j])
s = height[j] * (j - i);
else
s = height[i] * (j - i);
max = s > max ? s : max;
}
}
return max;
}
}

第二种解法:贪心算法

public class Solution {
public int maxArea(int[] height) {
int maxarea = 0, l = 0, r = height.length - 1;
while (l < r)
{
maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
if (height[l] < height[r])
l++;
else
r--;
}
return maxarea;
}
}

reference

https://leetcode.com/articles/container-with-most-water/

总结##

第一种暴力算法很容易想到,但时间复杂度为O(n^2);第二种使用的是贪心算法,时间复杂度为O(n)。

Notes

1.可以在暴力算法的基础上得到贪心算法;

LeetCode--Array--Container With Most Water (Medium)的更多相关文章

  1. [leetcode] 11. Container With Most Water (medium)

    原题链接 以Y坐标长度作为木桶边界,以X坐标差为桶底,找出可装多少水. 思路: 前后遍历. Runtime: 5 ms, faster than 95.28% of Java class Soluti ...

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

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

  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

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

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

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

  6. 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]

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

  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 ...

  10. [leetcode]11. Container With Most Water存水最多的容器

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

随机推荐

  1. ASP.NET MVC学习——控制器传递数据到view的简单案例

    从控制传数据到view端的方法: 方法一:修改Control数据,添加ViewBag 1.ViewBag.+ 任意变量名 = "你想要输入的数据" 2.在Control对应的csh ...

  2. Golang——详解Go语言代码规范

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Golang专题的第二篇,我们来看看Go的语言规范. 在我们继续今天的内容之前,先来回答一个问题. 有同学在后台问我,为什么说Gola ...

  3. nodejs一些比较实用的命令

    在学习node的时候是从express开始的,在express中有一个generate,如果在机器上面全局的安装了express-generate的话,可以直接实用[express project_n ...

  4. Cucumber(2)——目录结构以及基本语法

    目录 回顾 HelloWorld 扩展 回顾 在上一节中,我大致的介绍了一下cucumber的特点,以及基于ruby和JavaScript下关于cucumber环境的配置,如果你还没有进行相关的了解或 ...

  5. vue2.x学习笔记(十九)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12631022.html. 程序化的事件侦听器 在前面的学习中,我们已经知道了[$emit]全局属性的用法,它可以被 ...

  6. 手写一个简单的HashMap

    HashMap简介 HashMap是Java中一中非常常用的数据结构,也基本是面试中的"必考题".它实现了基于"K-V"形式的键值对的高效存取.JDK1.7之前 ...

  7. OAuth - 四种方式

    OAuth 2.0 的标准是 RFC 6749 文件.该文件先解释了 OAuth 是什么. OAuth 引入了一个授权层,用来分离两种不同的角色:客户端和资源所有者.......资源所有者同意以后,资 ...

  8. Websec level 30

    前言 昨天在易霖博搞的网络安全与执法竞赛看到的一道web题,实际上就是用两个原题凑起来的.. 不过后面的一关没见过这里简单记录一下 第一关 打开是个登录界面,和BJDCTF的简单注入一模一样,连密码都 ...

  9. 算法笔记刷题2(codeup 1928)

    又磕了一晚上,多点测试真的很烦 ,完全不知道错哪里,后来发现是我变量名命名不规范导致自己晕了填错了,其实思路还是对的 我觉得书上的做法也还行,但我不太喜欢用二维数组,所以拿以前写的算天数的程序改装了一 ...

  10. 宝塔利用git+ webhooks 实现git更新远程同步Linux服务器

    参考: https://blog.csdn.net/alipea/article/details/83858177 https://www.bt.cn/bbs/thread-5348-1-1.html ...