【LeetCode】011 Container With Most Water
题目:
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.
题解:
暴力解即可,注意这个面积是长方形的,我刚开始犯傻给写成梯形了。。。两个思路:两个for循环遍历,结果就是TLE了。另一个就是头尾指针遍历法,时间复杂度降为O(n).以后遇到这种需要遍历的,要先想到头尾指针法,而不是两个for循环(其实也是双指针,只是位置不一样)。同时,与之前遇到的数组问题一样,小幅度的优化就是在大循环内就跳过重复项
Solution 1 (TLE)
class Solution {
public:
int maxArea(vector<int>& height) {
int area = , n = height.size();
for(int i=; i<n-; i++) {
for(int j=i+; j<n; j++) {
int new_area = (j-i)*min(height[i], height[j]);
if(height[i]== || height[j]==) new_area = ;
area = max(area, new_area);
}
}
return area;
}
};
Solution 2
class Solution {
public:
int maxArea(vector<int>& height) {
int area = , i = , j = height.size() - ;
while (i < j) {
area = max(area, min(height[i], height[j]) * (j - i));
height[i] < height[j] ? ++i : --j;
}
return area;
}
};
Solution 3
class Solution {
public:
int maxArea(vector<int>& height) {
int area = ;
int i = , j = height.size() - ;
while (i < j) {
int h = min(height[i], height[j]);
area = max(area, (j - i) * h);
while (height[i] <= h && i < j) i++;
while (height[j] <= h && i < j) j--;
}
return area;
}
};
【LeetCode】011 Container With Most Water的更多相关文章
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】11. Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...
- 【leetcode】778. Swim in Rising Water
题目如下: 解题思路:本题题干中提到了一个非常重要的前提:"You can swim infinite distance in zero time",同时也给了一个干扰条件,那就是 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- Attribute "resultType" must be declared for element type "insert".
这是mybatis插入数据库之后出现的问题,至于为什么出现这个问题,是因为插入的时候你照抄了查询的语句,插入的时候只有id属性和parameterType属性,并没有“resultType”属性,要注 ...
- log4j入门及常用配置
<pre class="java" name="code">import org.apache.log4j.BasicConfigurator; ...
- IPv4(四)子网和子网掩码
为了完成路由选择,每个数据链路(网络)都必须有一个惟一的地址: 另外,数据链路上的每台主机也必须有一个地址,这个地址不仅标识主机为一个网络成员,还可以把主机与网络上的其他主机区分开来. 粗放使用IPv ...
- 串口设置-stty--设置终端线
stty - chang and print terminal line settings SYNOPSIS stty [-F DEVICE | --file=DEVICE] [SETTING]... ...
- Thymeleaf框架
简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在 ...
- 目标跟踪之粒子滤波---Opencv实现粒子滤波算法
目标跟踪学习笔记_2(particle filter初探1) 目标跟踪学习笔记_3(particle filter初探2) 前面2篇博客已经提到当粒子数增加时会内存报错,后面又仔细查了下程序,是代码方 ...
- uva 12083 Guardian of Decency (二分图匹配)
uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...
- 不错的iOS相关的主页或站点 (更新于14-06-22)
近期一直没事在翻一些站点看看资料学习下. 推荐几个不错的站点: http://www.raywenderlich.com/ 这个站点有各种各样的教程,可惜是大部分都是英文教程,只是阅读起来还好.每 ...
- 解决:IOS viewDidAppear/viewWillAppear无法被调用
本文转载至 http://my.oschina.net/lvlove/blog/82264 原因: 苹果的文档是这样描述的: If the view belonging to a view con ...
- Selenium 我的自动化测试历程 (Selenium+TestNG+Java+ReportNG+Jenkins)
测试环境:Java+Selenium+TestNG,Jenkins持续集成. 测试代码 代码结构 采用页面对象模型(Page Object),减少UI修改后,对代码的影响. Java编写,采用Te ...