Skip to content

Instantly share code, notes, and snippets.

@nimbus98
Created October 17, 2018 12:02
Show Gist options
  • Save nimbus98/425bbca1c079fa102cc3f6ba3c5f60a0 to your computer and use it in GitHub Desktop.
Save nimbus98/425bbca1c079fa102cc3f6ba3c5f60a0 to your computer and use it in GitHub Desktop.
IEEE CodeBuddy | Week 5 | Aditya A & Akash Nair
int Solution::trap(const vector<int> &A) {
int ans = 0;
int n = A.size();
int i = 0, j = n - 1;
int lm = -1, rm = -1;
while(i < j){
if(A[i] < A[j]){
if(A[i] > lm) lm = A[i];
else ans += (lm - A[i]);
i++;
}
else{
if(A[j] > rm) rm = A[j];
else ans += (rm - A[j]);
j--;
}
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment