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 5573 Binary Tree(构造题)
http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给出一个满二叉树,根节点权值为1,左儿子为2*val,右儿子为2*val+1.现在有只青蛙从根节点出 ...
- JAVA怎样理解面向对象
一.对象 现实世界中,随处可见的一种事物就是对象,对象是事物存在的实体,如人类.书桌.计算机.高楼大厦等.人类解决问题的方式总是将复杂的事物简单化,于是就会思考这些对象都是由哪些部分组成的.通常都 ...
- C#调用cmd命令
using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Windows ...
- 为DataGridView增加鼠标滚轮功能
#region 鼠标滚动 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "Wi ...
- _spellmod_on_learn、_spellmod_on_remove
_spellmod_on_learn._spellmod_on_remove,这俩表分别控制学习技能和遗忘技能时调用的GM命令.例如你制作了一个技能,学习炼金术这个专业时给玩家,遗忘炼金术这个专业时同 ...
- 性能跃升50%!解密自主研发的金融级分布式关系数据库OceanBase 2.0
小蚂蚁说: 相信大家对蚂蚁金服自主研发的金融级分布式关系数据库OceanBase的故事不再陌生了.在刚刚过去的2018年天猫双11中,成交额2135亿再次创造了新纪录,而支撑今年双11的支付宝核心链路 ...
- springboot + mybatis 的项目,实现简单的CRUD
以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD 上图是项目的 ...
- PHP直接将文件流转换为字符串
有时候不需要图片直接输出到浏览器,需要如下处理! 输出到浏览器 $qrCode = new QrCode(); $qrCode ->setText('Life is too short to b ...
- STL_map.插入
环境:Win7x64.vs08x86 1.类中这样声明:map<string, list<string>> FmapTagAttr; 2.插入数据时这样: list<st ...
- nRF52832定时器
1概述 定时器能够被配置为两种模式:定时模式和计数模式,nrf52832有五个定时器,timer0--timer4 . 2常用得函数 函数功能:初始化定时器 ret_code_t nrf_drv_ti ...