Skip to content

Instantly share code, notes, and snippets.

@prajwal27
Created October 29, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prajwal27/3ebb8e12ec26989d5e29f5dee6a32607 to your computer and use it in GitHub Desktop.
Save prajwal27/3ebb8e12ec26989d5e29f5dee6a32607 to your computer and use it in GitHub Desktop.
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