Created
October 17, 2018 12:02
-
-
Save nimbus98/425bbca1c079fa102cc3f6ba3c5f60a0 to your computer and use it in GitHub Desktop.
IEEE CodeBuddy | Week 5 | Aditya A & Akash Nair
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 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