LintCode 383: Max Area

题目描述

给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai)。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)(i, 0)。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。

样例

给出[1,3,2], 最大的储水面积是2.

Mon Feb 27 2017

思路

第一次看题目还以为是之前写的LintCode 510: Maximal Rectangle,后来发现还是不同的,最大矩形那题中间的矩形不能低于两边,而本题只需要考虑两边的高度,中间不用管。

最简单的当然就是两层遍历了,时间复杂度是 \(O(n^2)\),也能AC

还有一种时间复杂度为 \(O(n)\) 的方法,用一前一后两个指针扫描遍历,左右两条边的高度即可确定一个容器的容量。若大于当前的最大值,则更新最大值。下一步是较短的那条边步进一步,直到两者相遇。

代码

// 装最多水的容器
int maxArea(vector<int> &heights)
{
int i = 0, j = heights.size() - 1, max_vol = 0;
while(i < j)
{
int vol = (j - i) * min(heights[i], heights[j]);
if (max_vol < vol) max_vol = vol;
heights[i] < heights[j] ? ++i : --j;
}
return max_vol;
}

LintCode 383: Max Area的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. [Swustoj 24] Max Area

    Max Area 题目描述: 又是这道题,请不要惊讶,也许你已经见过了,那就请你再来做一遍吧.这可是wolf最骄傲的题目哦.在笛卡尔坐标系正半轴(x>=0,y>=0)上有n个点,给出了这些 ...

  3. [转][Swust OJ 24]--Max Area(画图分析)

    转载自:http://www.cnblogs.com/hate13/p/4160751.html Max Area 题目描述:(链接:http://acm.swust.edu.cn/problem/2 ...

  4. [leetcode]python 695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  6. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...

  7. LeetCode 695. Max Area of Island (岛的最大区域)

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. [LeetCode] Max Area of Island 岛的最大面积

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. [Swift]LeetCode695. 岛屿的最大面积 | Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. jndi连接数据库配置过程总结

    一.我们先找到tomcat安装目录中conf目录下的context.xml更改里面的内容: <?xml version='1.0' encoding='utf-8'?> <Conte ...

  2. [转帖]IBM POWER9 E950 and E980 Servers Launched

    IBM POWER9 E950 and E980 Servers Launched https://www.servethehome.com/ibm-power9-e950-and-e980-serv ...

  3. .net webapi创建接口

    最近使用webapi做了一个用户数据库接口,方便其它网站接入验证用户,实现中解决出现的一些问题,做了一些记录, 1.返回显示为json数据 2.允许其他网站访问,刚开始没有设,在本地机测试时可以访问, ...

  4. spring通过工厂模式解决页面耦合问题

    spring通过工厂模式解决页面耦合问题

  5. P2461 [SDOI2008]递归数列

    题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj 和 cj ...

  6. hbase 跳转过滤器skipfilter

    用于跳过整个行键,需要和其他过滤器一起使用,本例SkipFilter和ValueFilter过滤器组合使用过滤不符合条件的行, 如果不配合SkipFiter,ValueFilter只过滤单元值包含的列 ...

  7. MT【137】多少个?

    数列\(\{a_n\}\)共11项,\(a_1=0,a_{11}=4\),且\(|a_{k+1}-a_{k}|=2,k=1,2,\cdots,10\) 求满足条件的不同的数列的个数______ 解答: ...

  8. 洛谷 P4292 [WC2010]重建计划 解题报告

    P4292 [WC2010]重建计划 题目描述 \(X\)国遭受了地震的重创, 导致全国的交通近乎瘫痪,重建家园的计划迫在眉睫.\(X\)国由\(N\)个城市组成, 重建小组提出,仅需建立\(N-1\ ...

  9. apache.commons.io.FileUtils的常用操作

    至于相关jar包可以到官网获取 http://commons.apache.org/downloads/index.html package com.wz.apache.fileUtils; impo ...

  10. 在Struts2的Action中获得request response session几种方法

    转载自~ 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为se ...