收集雨水问题

直方图收集雨水问题:

给定n个非负整数,表示直方图的方柱的高度,同时,每个方柱的宽度假定都为1,若这样形状的容器收集雨水,可以盛多少雨水。

如:输入0,1,0,2,1,0,1,3,2,1,2,1;返回为6.

如图所示:

程序实现:

 1 #include <iostream>
 2 using namespace std;
 3
 4 int TrappingRainWater(int A[],int n){
 5     int SecHight = 0;//当前找到的第二大数
 6     int left = 0;
 7     int right = n-1;
 8     int trap = 0;//依次遍历每个方柱能装水的容量
 9     while(left<right){
10         if(A[left]<A[right]){
11             SecHight = max(SecHight,A[left]);
12             trap += (SecHight-A[left]);
13             left++;
14         }
15         else{
16             SecHight = max(SecHight,A[right]);
17             trap +=(SecHight-A[right]);
18             right--;
19         }
20     }
21     return trap;
22 }
23 int main()
24 {
25     int A[] ={0,1,0,2,1,0,1,3,2,1,2,1};
26     cout<<TrappingRainWater(A,sizeof(A)/sizeof(int))<<endl;
27     return 0;
28 }

运行结果:

转载请注明出处:

C++博客园:godfrey_88

http://www.cnblogs.com/gaobaoru-articles/

时间: 2024-08-07 08:37:18

收集雨水问题的相关文章

[LeetCode]101. 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 a

leetcode python 042收集雨水

'''给定n个非负整数表示每个条的宽度为1的高程图,计算下雨后能够捕获多少水.例如,鉴于[0,1,0,2,1,0,1,3,2,1,2,1],返回6.这个题要先算出盛满水后的高程图,减去前者就是雨水.盛水多高取决于左右最高的两处低的一方.'''l1=[0,1,0,2,1,0,1,3,2,1,2,1]w=[]for i in range(len(l1)):    w.append(min(max(l1[0:i+1]),max(l1[i:]))-l1[i])print('收集雨水:',sum(w))

[LintCode] 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. Have you met this question in a real interview? Yes Example Given [0,1,0,2,1,0,1,3,2,1,2,1], return

【LeetCode】042 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

[LeetCode] 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 containe

Trapping Rain Water -- leetcode

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 a

An Easy Problem?!(细节题,要把所有情况考虑到)

http://poj.org/problem?id=2826 An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10505   Accepted: 1584 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails two woode

[LeetCode] 11. 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 containe

程序猿爱上了经济学《经济学原理》

在大段经济学内容之前,先举出几个很有意思的现象.观点和问题:  1.安全带减少了每次车祸的死亡人数,但却增加了车祸的次数.净结果是司机死亡人数变动很小,而行人死亡人数增加了.  2.设想你对看一场新放映的电影的评价是10美元.你用7美元买了一张票,但在进电影院之前,你丢了票.你应该再买一张吗?或者你应该马上回家并拒绝花14美元看电影?回答是你应该再买一张票.  3.为什么好木匠赚的钱只比一般木匠多一些,好的管道工赚的钱只比一般管道工多一些,但是明星赚的钱比一般的演员却多了几十倍,几百倍?  4.