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. IE8+SpringMVC文件上传防止JSON下载

    今天在IE8测试文件上传的时候发现总是提示下载,原因是上传接口返回的是json,通过以下修改就可以保证返回是json并且不会出现下载的情况: @RequestMapping(value = " ...

  2. 复利计算测试(C语言)

    对我们和复利计算程序,写单元测试. 有哪些场景? 期待的返回值 写测试程序. 运行测试. 测试模块 测试输入 预期结果 运行结果 bug跟踪 计算终值 (本金,年限,利率) 终值     1 (100 ...

  3. PAT 甲级 1155 Heap Paths

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...

  4. #Leetcode# 817. Linked List Components

    https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...

  5. nginx负载均衡和tomcat热部署简单了解

    简单说下几个名词 nginx     它是一个反向代理,实际上就是一台负责转发的代理服务器,貌似充当了真正服务器的功能,但实际上并不是,代理服务器只是充当了转发的作用,并且从真正的服务器那里取得返回的 ...

  6. ini_set的权限大于error_reporting

    在用php做网站开发的时候 , 为防止用户看到错误信息,而出现的不友好界面.故一般性会在php.ini里设置:display_errors = Off;不过在开发的时候,我们有时候需要打开错误信息.这 ...

  7. 从解决一个java.lang.NoSuchMethodError想到的

    今天在发布系统部署一个web app的时候,发现应用服务器(tomcat 7.0.26)不能正常启动,于是远程登陆到服务器上查看应用服务器的启动日志,在tomcat_home的logs/localho ...

  8. Best Time to Buy and Sell Stock IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. C++中unique函数

    目录 介绍 用法举例 数组 vector 介绍 unique是STL比较实用的一个函数.用于"去除"容器内相邻的重复的元素(只保留一个).这里说的去除并不是真正将容器内的重复元素删 ...

  10. Linux系统中/opt 和 /usr目录

    重点:usr是Unix Software Resource的缩写,即“UNIX操作系统软件资源”所放置的目录. 下面是个人找到的适合类似我这种从Windows转向Linux小白的文章. Ref:htt ...