LeetCode题解——Container With Most Water
题目:
x轴上有一些点,每个点上有一条与x轴垂直的线(线的下端就是这个点,不超出x轴),给出每条线的高度,求这些线与x轴组成的最大面积。
解法:
贪心策略,维持两个指针,分别指向第一个和最后一个元素,对于其中小的一个,它所能围成的最大面积就是到另一个元素之间,所以小的一个要往中间走一步。
代码:
class Solution {
public:
int maxArea(vector<int> &height) { //O(n)
int result = , cap; //cap是当前所围成的面积
int start = , end = height.size() - ;
while(start < end)
{
cap = min(height[start], height[end]) * (end - start); //当前的面积,其高度由小的一个决定
result = max(result, cap);
if(height[start] >= height[end]) //小的那个指针往中间移动
--end;
else
++start;
}
return result;
}
};
LeetCode题解——Container With Most Water的更多相关文章
- [LeetCode 题解]: Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- leetcode题解||Container With Most Water问题
problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode(11)题解: Container With Most Water
https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- Shuffle和排序
MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...
- printf left justify
http://www.lemoda.net/c/printf-left-justify/index.html This example program demonstrates how to left ...
- Dozer应用——类之间值的映射
1. Mappings via Annotation public class SourceBean { private Long id; private String name; @Mapping( ...
- i386 和amd64 的意思
首先可以简化一个概念,i386=Intel 80386.其实i386通常被用来作为对Intel(英特尔)32位微处理器的统称. Windows NT类系统的安装盘上,通常i386是其根上的一个文件夹, ...
- 自定义View(2)canas绘制基本图形的示例
效果 代码: void drawSample(Canvas canvas) { /* * 方法 说明 drawRect 绘制矩形 drawCircle 绘制圆形 drawOval 绘制椭圆 drawP ...
- BZOJ3122 随机数生成器
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3122 题意: 思路:(1)x1=t则n=1; (2)a=0,则b=t,n=2;否则无解: ...
- 转:java提取图片中的像素
本文转自:http://www.infosys.tuwien.ac.at/teaching/courses/WebEngineering/References/java/docs/api/java/a ...
- svn url does not contain valid patch
想把项目上传到svn上,由于误点击了apply patch.所以出现下面的错误. 正确做法是在项目上右击找到Team----share Project 如图: 点击share project后出现如图 ...
- 函数xdes_set_bit
/**********************************************************************//** Sets a descriptor bit of ...
- BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1627 网格图,给出起点,终点,障碍,求最短路. 分析 简单的宽搜. #include < ...