Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created January 4, 2019 03:53
Show Gist options
  • Save ttsugriy/81744e62aed3f84bdd58a0ffe9c79fcb to your computer and use it in GitHub Desktop.
Save ttsugriy/81744e62aed3f84bdd58a0ffe9c79fcb to your computer and use it in GitHub Desktop.
Maximum width ramp (leetcode 962)
class Solution {
public:
int maxWidthRamp(vector<int>& A) {
map<int, int> largest;
largest[A.back()] = A.size()-1;
int max_width = 0;
for (int i = A.size()-2; i >= 0; --i) {
auto found = largest.lower_bound(A[i]);
if (found != largest.cend()) {
max_width = max(max_width, found->second - i);
}
if (A[i] > largest.rbegin()->first) {
largest[A[i]] = i;
}
}
return max_width;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment