|
| 1 | +// 962. Maximum Width Ramp |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/maximum-width-ramp/ |
| 5 | +// |
| 6 | +// Tags: Array - Stack - Monotonic Stack |
| 7 | + |
| 8 | +struct Solution; |
| 9 | +impl Solution { |
| 10 | + /// One pass using monotonic stack, but for each value iterate the monotonic stack back while |
| 11 | + /// the top of the stack is less or equal to the current value. |
| 12 | + /// |
| 13 | + /// Time complexity: O(n) |
| 14 | + /// Space complexity: O(n) |
| 15 | + /// |
| 16 | + /// Runtime 8 ms Beats 20% |
| 17 | + /// Memory 2.86 MB Beats 20% |
| 18 | + #[allow(dead_code)] |
| 19 | + pub fn max_width_ramp_one_pass(nums: Vec<i32>) -> i32 { |
| 20 | + let mut seen = vec![0]; |
| 21 | + let mut res = 0; |
| 22 | + for i in 1..nums.len() { |
| 23 | + let mut seen_idx = seen.len() - 1; |
| 24 | + if nums[i] < nums[seen[seen_idx]] { |
| 25 | + seen.push(i); |
| 26 | + } else { |
| 27 | + while nums[i] >= nums[seen[seen_idx]] { |
| 28 | + res = res.max(i - seen[seen_idx]); |
| 29 | + if seen_idx == 0 { |
| 30 | + break; |
| 31 | + } |
| 32 | + seen_idx -= 1; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + res as _ |
| 37 | + } |
| 38 | + |
| 39 | + /// Two passes, create a monotonic non-increasing stack on the first one, on the second pass, |
| 40 | + /// find indexes of the greatest ramp. |
| 41 | + /// |
| 42 | + /// Time complexity: O(n) |
| 43 | + /// Space complexity: O(n) |
| 44 | + /// |
| 45 | + /// Runtime 7 ms Beats 20% |
| 46 | + /// Memory 2.77 MB Beats 20% |
| 47 | + pub fn max_width_ramp(nums: Vec<i32>) -> i32 { |
| 48 | + let mut stack = vec![]; |
| 49 | + for i in 0..nums.len() { |
| 50 | + if stack.is_empty() || nums[stack[stack.len() - 1]] > nums[i] { |
| 51 | + stack.push(i); |
| 52 | + } |
| 53 | + } |
| 54 | + let mut res = 0; |
| 55 | + for i in (0..nums.len()).rev() { |
| 56 | + while !stack.is_empty() && nums[*stack.last().unwrap()] <= nums[i] { |
| 57 | + res = res.max(i - stack.pop().unwrap()); |
| 58 | + } |
| 59 | + } |
| 60 | + res as _ |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Tests. |
| 65 | +fn main() { |
| 66 | + let tests = [ |
| 67 | + (vec![6, 2], 0), |
| 68 | + (vec![6, 0, 8, 2, 1, 5], 4), |
| 69 | + (vec![9, 8, 1, 0, 1, 9, 4, 0, 4, 1], 7), |
| 70 | + ]; |
| 71 | + println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len()); |
| 72 | + let mut success = 0; |
| 73 | + for (i, t) in tests.iter().enumerate() { |
| 74 | + let res = Solution::max_width_ramp(t.0.clone()); |
| 75 | + if res == t.1 { |
| 76 | + success += 1; |
| 77 | + println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i); |
| 78 | + } else { |
| 79 | + println!( |
| 80 | + "\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m", |
| 81 | + i, t.1, res |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + println!(); |
| 86 | + if success == tests.len() { |
| 87 | + println!("\x1b[30;42m✔ All tests passed!\x1b[0m") |
| 88 | + } else if success == 0 { |
| 89 | + println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m") |
| 90 | + } else { |
| 91 | + println!( |
| 92 | + "\x1b[31mx\x1b[95m {} tests failed!\x1b[0m", |
| 93 | + tests.len() - success |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
0 commit comments