Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created January 4, 2019 07:58
Show Gist options
  • Save ttsugriy/d106f66e30bd8db704ef96d41b4ae6d4 to your computer and use it in GitHub Desktop.
Save ttsugriy/d106f66e30bd8db704ef96d41b4ae6d4 to your computer and use it in GitHub Desktop.
impl Solution {
pub fn max_width_ramp(a: Vec<i32>) -> i32 {
let mut largest: Vec<usize> = vec![(a.len()-1) as usize];
for (i, val) in a.iter().enumerate().rev().skip(1) {
if *val > a[*largest.last().unwrap()] {
largest.push(i);
}
}
let mut max_width = 0;
for (i, val) in a.iter().enumerate() {
while !largest.is_empty() && *val <= a[*largest.last().unwrap()] {
max_width = std::cmp::max(max_width, largest.pop().unwrap() - i);
}
}
max_width as i32
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment