42. Trapping Rain Water (Array,stack; DP)
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!
思路:动态规划。第一次存储从开始到i最高的位置,求最高位置a之前的存水量=a与a之前最高位置b之间的水量+b与b之前最高位置c之间的水量...
第二次存储从末尾到i最高的位置,求最高位置a之后的存水量=a与a之前最高位置m之间的水量+m与m之前最高位置n之间的水量...
- class Solution {
- public:
- int trap(vector<int>& height) {
- int size = height.size();
- if(size==) return ;
- vector<int> dp(size,); //save the highest position until now
- int ret = ;
- int left,right;
- //first traverse from left to right
- for(int i = ; i < size; i++){
- //state transfer
- if(height[i] > height[dp[i-]]) dp[i]=i;
- else dp[i] = dp[i-];
- }
- //calculate the water to the left of the highest position
- left = dp[size-];
- while(left>){
- right=left;
- left=dp[right-];
- for(int i = left+; i < right; i++){
- ret += (height[left]-height[i]);
- }
- }
- //second traverse from right to highest pos
- int highestPos=dp[size-];
- dp[size-]=size-;
- for(int i = size-; i >= highestPos; i--){
- //state transfer
- if(height[i] > height[dp[i+]]) dp[i]=i;
- else dp[i] = dp[i+];
- }
- //calculate the water to the right of the highest position
- right=highestPos;
- while(right<size-){
- left=right;
- right=dp[left+];
- for(int i = left+; i < right; i++){
- ret += (height[right]-height[i]);
- }
- }
- return ret;
- }
- };
改进:用stack代替vector作为状态存储。stack的栈顶是到目前为止最大元素的下标,因为最高位置是关键,找到最高位置,可以往左,往右计算水位。
stack的实现类似用两个stack实现能够返回最大元素的stack。
- class Solution {
- public:
- int trap(vector<int>& height) {
- int size = height.size();
- if(size==) return ;
- stack<int> s;
- s.push();
- int i, ret = , highestPos;
- //First traverse from left to right
- for(int i = ; i < size; i++){
- if(height[i]<=height[s.top()]) continue;
- s.push(i);
- }
- i=s.top();
- highestPos = i;
- while(){
- if(i==s.top()){
- s.pop();
- if(s.empty()) break;
- }
- else{
- ret+=(height[s.top()]-height[i]);
- }
- i--;
- }
- //then traverse from right to left
- s.push(size-);
- for(int i = size-; i >= highestPos; i--){
- if(height[i]<=height[s.top()]) continue;
- s.push(i);
- }
- i=highestPos;
- while(){
- if(i==s.top()){
- s.pop();
- if(s.empty()) break;
- }
- else{
- ret+=(height[s.top()]-height[i]);
- }
- i++;
- }
- return ret;
- }
- };
42. Trapping Rain Water (Array,stack; DP)的更多相关文章
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- [LeetCode] 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(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- 学习笔记之Moq
dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...
- shell_sctipts: 删除mysql备份到最后7日
目前,mysql的备份文件,经过一周左右清理,手动清理会比较费事,所以写了一个简单脚本来实现. 前提介绍: mysql备份文件放在/usr/bak/sql里面,sql文件的备份名称格式为: mysql ...
- 新生代老年代GC组合
新生代通常存活时间较短,因此基于Copying算法来进行回收,所谓Copying算法就是扫描出存活的对象,并复制到一块新的完全未使用的空间中 在执行机制上JVM提供了串行GC(SerialGC).并行 ...
- [UE4]动画序列面板
- 不重启修改'log_slave_updates'变量
Variable 'log_slave_updates' is a read only variable 不重启修改mysql变量 执行复制的时候遇到的问题 mysql> show variab ...
- Impala源码分析
问题导读:1.Scheduler任务中Distributed Plan.Scan Range是什么?2.Scheduler基本接口有哪些?3.QuerySchedule这个类如何理解?4.Simple ...
- Solr优化案例分析
随着umc接入主机的数量越来越多,每天产生的syslog日志数量也在剧增, 之前一天产生的syslog数量才不 到1W,随着整个集团的网络设备不端接入,导致现在每天产生的syslog数量大概在180w ...
- python中函数的返回值
函数返回值(一) <1>“返回值”介绍 现实生活中的场景: 我给儿子10块钱,让他给我买包烟.这个例子中,10块钱是我给儿子的,就相当于调用函数时传递到参数,让儿子买烟这个事情最终的目标是 ...
- python 编写远程连接服务器脚本
import paramiko client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPoli ...