Created
January 4, 2019 03:53
-
-
Save ttsugriy/81744e62aed3f84bdd58a0ffe9c79fcb to your computer and use it in GitHub Desktop.
Maximum width ramp (leetcode 962)
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
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