Created
October 29, 2018 02:56
-
-
Save prajwal27/3ebb8e12ec26989d5e29f5dee6a32607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Solution::trap(const vector<int> &a) { | |
int n = a.size(), ans = 0; | |
vector<int> l(n); | |
vector<int> r(n); | |
l[0] = a[0]; | |
for (int i = 1; i < n; i++) | |
left[i] = max(l[i-1], a[i]); | |
r[n-1] = a[n-1]; | |
for (int i = n-2; i >= 0; i--) | |
r[i] = max(r[i+1], a[i]); | |
for (int i = 0; i < n; i++) | |
ans += min(l[i],r[i]) - a[i]; | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment