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 ...
随机推荐
- go 一波走起
$ go run helloworld.go 运行 $ go build helloworld.go 编译 该命令生成一个名为helloworld的可执行的二进制文件,可以随时运行它 $ ./hell ...
- Oracle imp导入数据
拿到别人给的dmp文件如何导入自己的库中呢?上码: 代码: imp dms/123321@orcl file=F:\TP_ZG_20171208.DMP feedback=10000 buffer=1 ...
- 点击返回上一页 wx.navigateTo不管用了
做跳转的时候,发现想返回上一页,但是这个上一页又是tab上的页面,返回不了怎么办呢 wx.navigateTo({ url: '../search/search', }) 解决方法: wx.reL ...
- python中while循环运算符及格式化输出
一,while循环 while 条件: while语句块(循环体) 运行: 判断你给的条件是否为真,如果真则执行循环体.否则跳出循环. 执行完循环体之后再次判断条件是否为真 例子1 我们玩联盟的时候喷 ...
- 【NPOI】WebAPI-使用NPOI导出Excel
1.安装nuget包 2.封装方法 public byte[] ExportToByteArray(IEnumerable<string> headerText, IEnumerable& ...
- Oracle(限定查询2)
3.2 对数据进行限定查询 在标准SQL之中定义了许多的运算符. 3.2.1.关系运算符 范例: 范例: 范例: 在使用关系运算符判断字符数据的时候注意大小写的编写问题.因为Oracle是区分大小写的 ...
- eclipse里安装SVN插件的两种方式
eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 使用eclipse 里Help菜单的“Install New Software”,通过 ...
- openModelica调试
1打印信息 Modelica.Utilities.Streams.print(“messge”);
- vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations
1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...
- 总要先爬出坑的JEE架构
先来看看官网对它的定义. Java平台企业版(Java EE)是社区驱动的企业软件的标准.Java EE是使用Java Community Process开发的,其中包括来自行业专家,商业和开源组织, ...