Created
January 4, 2019 07:58
-
-
Save ttsugriy/d106f66e30bd8db704ef96d41b4ae6d4 to your computer and use it in GitHub Desktop.
Maximum width ramp in Rust (https://leetcode.com/problems/maximum-width-ramp/)
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
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