leetcode problem 42 -- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
思路:
这一道题类似于leetcode 11 题Container With Most Water. 要求最多能装多少水.有两种思路.
思路一:
先从左往右遍历.每遍历一个数字,就贪心得到这个数字所在的格子能最多装水. 由于是从左到右遍历,我们只考虑的左边的高度,没有考虑右边的高度(实际应该选择两者小的那个). 因此需要再从右往左遍历,同样贪心这个数字所在格子
最多能装多少水,这次以右边高度为准. 最终综合从左到右和从右到左的结果,取两者小的.
代码:(runtime 19ms)
class Solution {
public:
int trap(vector<int>& height) {
vector<int> leftToRight;
vector<int> rightToLeft;
aux_function(height.begin(), height.end(), leftToRight); aux_function(height.rbegin(), height.rend(), rightToLeft); int ans = ;
auto it1 = leftToRight.begin();
auto it2 = rightToLeft.rbegin(); while (it1 != leftToRight.end()){
ans += min(*it1++, *it2++);
}
return ans;
} private:
template<class Iter>
void aux_function(Iter begin, Iter end, vector<int> &ret) {
int left = ;
for (auto it = begin; it != end; it++) {
left = left > *it ? left : *it;
ret.push_back(left - *it);
}
}
};
思路二:
如上图中所示, 先求黑色格子与蓝色格子的总面积,然后再减去黑色各自面积.
代码:(runtime 10ms)
class Solution {
public:
int trap(vector<int> A) {
int n = A.size();
int summap = ;
int sumtot = ; for(int i = ; i < n; i++) summap += A[i]; int left = , right = n - ;
int leftbar = , rightbar = ;
while(left <= right) {
leftbar = max(A[left], leftbar);
rightbar = max(A[right], rightbar); if(leftbar <= rightbar) {
sumtot += leftbar;
left++;
//right--;
} else {
sumtot += rightbar;
right--;
//left++;
}
} return sumtot - summap;
}
};
leetcode problem 42 -- Trapping Rain Water的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- 【一天一道LeetCode】#42. Trapping Rain Water
一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- LeetCode OJ 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- PS Studio调用.exe输出错误信息的解决办法
在一个button_click下调用了如下外部可执行文件: $button1_Click = { #TODO: Place custom script here .\PsExec.exe \\192. ...
- VPN介绍--虚拟网络
VPN属于远程访问技术,简单地说就是利用公网链路架设私有网络.例如 公司员工出差到外地,他想访问企 原理 业内网的 服务器资源,这种访问就属于远程访问.怎么才能让外地员工访问到内网资源呢?VPN的 ...
- 面试体验:Microsoft 篇(转)
http://www.cnblogs.com/cathsfz/archive/2012/08/14/microsoft-interview-experience.html 在上一篇<面试体验:G ...
- android学习——popupWindow 在指定位置上的显示
先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. [java] view plaincopy private void showPopupWindow(View pare ...
- mysql:慢查询日志slow_query_log
1.慢查询日志:当查询超过一定的时间没有返回结果的时候,才会记录到慢查询日志中.默认不开启.采样的时候手工开启.可以帮助DBA找出执行慢的SQL语句 2.常用的参数详解: 注意:修改以下参数,需要重新 ...
- 用grunt搭建自动化的web前端开发环境-完整教程
原稿:http://www.cnblogs.com/wangfupeng1988/p/4561993.html#!comments jQuery在使用grunt,bootstrap在使用grunt,百 ...
- 不关闭seLinux解决vsftpd服务本地用户不能登录问题(500 OOPS: cannot change directory:/home/***
这里不讲vsftpd的基本配置,网上教程已经太多了.这里只说seLinux的问题. 日前在CentOS6.5中安装了vsftpd,按照网上搜索的教程,配置好/etc/vsftpd/vsftpd.con ...
- 陷阱~SQL全表扫描与聚集索引扫描
SqlServer中在查询时,我们为了优化性能,通常会为where条件的字段建立索引,如果条件比较固定还会建立组合索引,接下来,我们来看一下索引与查询的相关知识及相关陷阱. SQL表自动为主键加聚集索 ...
- 常用经典SQL语句大全(提升)
二.提升 1.说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用) 法一:select * into b from a where 1<>1(仅用于SQlServer) ...
- jquery实现asp.net 网页鼠标所在位置
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mouseposition. ...