Skip to content

Commit e67552e

Browse files
committed
LC 962. Maximum Width Ramp (Rust)
1 parent 5686421 commit e67552e

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ to the solution in this repository.
432432
| [953. Verifying an Alien Dictionary][lc953] | 🟢 Easy | [![python](res/py.png)][lc953py] [![rust](res/rs.png)][lc953rs] |
433433
| [956. Tallest Billboard][lc956] | 🟠 Medium | [![python](res/py.png)][lc956py] |
434434
| [958. Check Completeness of a Binary Tree][lc958] | 🟠 Medium | [![python](res/py.png)][lc958py] [![rust](res/rs.png)][lc958rs] |
435+
| [962. Maximum Width Ramp][lc962] | 🟠 Medium | [![rust](res/rs.png)][lc962rs] |
435436
| [967. Numbers With Same Consecutive Differences][lc967] | 🟠 Medium | [![python](res/py.png)][lc967py] |
436437
| [968. Binary Tree Cameras][lc968] | 🔴 Hard | [![python](res/py.png)][lc968py] |
437438
| [973. K Closest Points to Origin][lc973] | 🟠 Medium | [![python](res/py.png)][lc973py] |
@@ -1713,6 +1714,8 @@ to the solution in this repository.
17131714
[lc958]: https://leetcode.com/problems/check-completeness-of-a-binary-tree/
17141715
[lc958py]: leetcode/check-completeness-of-a-binary-tree.py
17151716
[lc958rs]: leetcode/check-completeness-of-a-binary-tree.rs
1717+
[lc962]: https://leetcode.com/problems/maximum-width-ramp/
1718+
[lc962rs]: leetcode/maximum-width-ramp.rs
17161719
[lc967]: https://leetcode.com/problems/numbers-with-same-consecutive-differences/
17171720
[lc967py]: leetcode/numbers-with-same-consecutive-differences.py
17181721
[lc968]: https://leetcode.com/problems/binary-tree-cameras/

leetcode/maximum-width-ramp.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)