trap rain water

1 class Solution {
 2 public:
 3     int trap(int A[], int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if( n==0 )return 0;
 7         int volum = 0;
 8         stack<int> s;
 9         s.push(0);
10         int t;
11         for(int i=1;i<n;i++)
12         {
13             if( A[i] == 0 ) continue;
14             int h = 0;
15             while(!s.empty() )
16             {
17                 t = s.top();
18                 volum += (i-t-1)*abs( min(A[i], A[t]) - h );
19                 if( A[i] >= A[t] )
20                 {
21                     s.pop();
22                 //    s.push(i);
23                     h = A[t];
24                 }
25                 else
26                     break;              
27             }   
28             s.push(i);
29         }
30         return volum;
31         
32     }
33 };

原文链接: https://www.cnblogs.com/jumpinGGrass/p/3173443.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/94723

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午2:46
下一篇 2023年2月10日 上午2:46

相关推荐