[LeetCode] Container With Most Water 简要分析
前言
这题非要说贪心的话也算是吧,不过最主要的特征还是双指针。LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的。这道题倒是“短板效应”的不错体现了。
题目
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.
思路
图片看不清楚的话可右键复制链接转到图床再看。
代码
class Solution {
public:
int maxArea(vector<int>& height) {
int first = ;
int end = height.size() - ;
int result = INT_MIN;
while (first < end)
{
int width = end - first;
int board = height[end]<height[first]?height[end]:height[first];
int area = board*width;
result = result>area?result:area;
if (height[first] <= height[end])
first++;
else
end--;
}
return result;
}
};
其他
JAVA果然过得比CPP快,Python和C#依旧垫底。
[LeetCode] Container With Most Water 简要分析的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- Leetcode:Container With Most Water分析和实现
题目大意是提供n个非负整数,a1,...,an,可以将其理解为多个边界(垂直的线段),其中ai处于x坐标i处,并且ai代表的线段高度即为ai的值.要求我们取这样的i与j,使得ai与aj以及x坐标轴围成 ...
- [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, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- 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 ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 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 (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
随机推荐
- cojs EX_香蕉 题解报告
这道题目是香蕉的加强版 当m=100w时矩阵会很大,而且又有多组询问,所以这道题用原来香蕉的程序会T 所以我们需要更好点的做法 我们考虑优化我们的状态 首先考虑这道题的隐藏性质,考虑不合法的情况 那么 ...
- Spring3 报org.aopalliance.intercept.MethodInterceptor问题解决方法
原文:Spring3 报org.aopalliance.intercept.MethodInterceptor问题解决方法 一 开发环境:JDK5+Spring3.0.5+Myeclipse6.6+T ...
- IntelliJ IDEA 15开发Java Maven项目
1.安装好之后开始创建项目
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
Deep Neural Networks are the more computationally powerful cousins to regular neural networks. Learn ...
- Linux 删除文件夹和创建文件的命令
删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例: rm -f /var/log ...
- 无法生成临时类(result=1)。 error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性
对于错误无法生成临时类(result=1).error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性 出现这个错 ...
- Windows 7下配置JDK环境变量和Java环境变量配置
下面来介绍一下Java环境变量配置,是在Windows 7下配置JDK环境变量. 方法/步骤 1 安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为:C:\Program Fil ...
- C#ShowCursor光标的显示与隐藏
使用using System.Runtime.InteropServices; [DllImport("user32.dll" , EntryPoint = "Sho ...
- Altium designer总结
itwolf原创文章,转载请注明出处 大概有半年没有画过PCB板了,最近突然又要画一个简单的小板子,却发现好多东西已经不是很熟练了,现在把Altium designer软件的使用中要注意的问题和一些小 ...
- HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)
Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...