C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做。感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过。每天都要被LeetCode打击一次。自“抱”自“泣”,逻辑推理能力太差了。
题目:
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.
面积取决于两个因素,一个是两个点的横坐标差值,另一个是两个点中的最小值。设置两个指针,分别表示数组的头和尾,首先计算这两个点的所构成长方形的面积,然后移动两点中较小数的指针向前(头指针)或退后(尾指针),因为如果移动的是两点中较大那个数的指针的话,两个点横坐标变小,两个点中最小值变小或者不变,那么长方形面积肯定变小,这样的计算是多余的。重复上述过程知道首尾指针相遇,记录该过程中的面积最大值就是结果。
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0, j = height.size()-1;
int current = 0, bigest = 0;
while(i<j){
current = (j-i) * (height[i]>height[j]?height[j--]:height[i++]);
bigest = bigest>current?bigest:current;
}
return bigest;
}
};
看了一下运行最快的代码,思路一样,估计测试用例不一样导致运行时间不一样。
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 装最多水的容器
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 ...
- 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 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- [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 ...
- [Leetcode] container with most water 最大水容器
Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...
随机推荐
- HDU 3401 Trade(斜率优化dp)
http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...
- 初步:jenkins自动构建安卓Apk
1:本地搭建jenkins 2:下载插件 3:配置相关信息(git,sdk等等) 3:拉取git仓库代码 4:编译执行 参考文章:http://www.cnblogs.com/reblue520/p/ ...
- 【BZOJ】3527: [Zjoi2014]力
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 把${f_i}$消去之后换元就是卷积的形式,直接算就可以了. #include< ...
- Mysql 查看定时器 打开定时器 设置定时器时间
1.查看是否开启evevt与开启evevt. 1.1.MySQL evevt功能默认是关闭的,可以使用下面的语句来看evevt的状态,如果是OFF或者0,表示是关闭的. show VARIABLES ...
- GZip对字符串压缩和解压
/// <summary> /// 压缩 /// </summary> /// <param name="value">需要压缩字符串</ ...
- Java中有多个异常, 如何确定捕获顺序(多个catch),先从上到下执行,判断异常的大小,如果包含捕到异常,就进入这个catch,后面的就不再执行
Java中异常的捕获顺序(多个catch)( Java代码 import java.io.IOException; public class ExceptionTryCatchTest { publi ...
- Anroid 搭建Maven私服(Android Studio)
一.场景 ① 公司中有多个项目,多人开发,开发中的某一模块需要被其他几个项目的模块所依赖,最简单粗暴的方式就是打包成aar或者jar文件逐个拷贝到libs下进行依赖,可是这种方式太过于麻烦,而且需要每 ...
- python 爬虫利器 Beautiful Soup
python 爬虫利器 Beautiful Soup Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文 ...
- jq 折面板+tab切换(自己封装的插件哦!!)
如上图所示的一个折面板效果+tab切换:最重要的js代码如下: 对于布局简单介绍下: hot_wrap_li 这个是带箭头的横条: Arrow 这个是箭头的div:hot_wrap_li_wrap 这 ...
- python标准库中socket模块详解
包含原理就是tcp的三次握手 http://www.lybbn.cn/data/datas.php?yw=71 这篇讲到了socket和django的联系 https://www.cnblogs.co ...