Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Trapping rain water problem
#include <bits/stdc++.h> using namespace std; int main() { //code int t; cin>>t; while(t--) { int n; cin>>n; if(n<=2) { cout<<"0"<<endl; } else { vector<int>v(n); for(int i=0;i<n;i++) cin>>v[i]; vector<int>lft(n); vector<int>rgt(n); lft[0]=v[0]; rgt[n-1]=v[n-1]; for(int i=1;i<n;i++) lft[i]=max(lft[i-1],v[i]); for(int i=n-2;i>=0;i--) rgt[i]=max(rgt[i+1],v[i]); int res=0; for(int i=0;i<n;i++) { res+=(min(lft[i],rgt[i])-v[i]); } cout<<res<<endl; } } return 0; }
run
|
edit
|
history
|
help
0
kadane's algorithm 2
lab17feb22x4B.cpp
Teatime Snack
Deque RotateString
Dar
Test 4(2017)
ClassQuiz
c2p_update
water drop/ water land
generate BinTree from sorted list