Skip to content

Commit 7577759

Browse files
committed
LC 946. Validate Stack Sequences (Rust Stack)
1 parent 31cdf04 commit 7577759

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
337337
| [936. Stamping The Sequence][lc936] | 🔴 Hard | [![python](res/py.png)][lc936py] |
338338
| [938. Range Sum of BST][lc938] | 🟢 Easy | [![python](res/py.png)][lc938py] |
339339
| [944. Delete Columns to Make Sorted][lc944] | 🟢 Easy | [![python](res/py.png)][lc944py] |
340-
| [946. Validate Stack Sequences][lc946] | 🟠 Medium | [![python](res/py.png)][lc946py] |
340+
| [946. Validate Stack Sequences][lc946] | 🟠 Medium | [![python](res/py.png)][lc946py] [![rust](res/rs.png)][lc946rs] |
341341
| [947. Most Stones Removed with Same Row or Column][lc947] | 🟠 Medium | [![python](res/py.png)][lc947py] |
342342
| [948. Bag of Tokens][lc948] | 🟠 Medium | [![python](res/py.png)][lc948py] |
343343
| [953. Verifying an Alien Dictionary][lc953] | 🟢 Easy | [![python](res/py.png)][lc953py] [![rust](res/rs.png)][lc953rs] |
@@ -1175,6 +1175,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
11751175
[lc944py]: leetcode/delete-columns-to-make-sorted.py
11761176
[lc946]: https://leetcode.com/problems/validate-stack-sequences/
11771177
[lc946py]: leetcode/validate-stack-sequences.py
1178+
[lc946rs]: leetcode/validate-stack-sequences.rs
11781179
[lc947]: https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/
11791180
[lc947py]: leetcode/most-stones-removed-with-same-row-or-column.py
11801181
[lc948]: https://leetcode.com/problems/bag-of-tokens/

leetcode/validate-stack-sequences.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 946. Validate Stack Sequences
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/validate-stack-sequences/
5+
//
6+
// Tags: String - Stack - Simulation
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Use an extra stack of memory and simulate the operations that took
11+
/// place, push the next element, then, while the top of the stack matches
12+
/// the next element that needs to be popped, pop it.
13+
///
14+
/// Time complexity: O(n) - We visit each element on the pushed array and
15+
/// do amortized O(1) for each.
16+
/// Space complexity: O(n) - The stack can grow to the same size as the
17+
/// input.
18+
///
19+
/// Runtime 0 ms Beats 100%
20+
/// Memory 2.1 MB Beats 90%
21+
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
22+
let mut stack = vec![];
23+
let mut pop_idx = 0;
24+
for val in pushed {
25+
stack.push(val);
26+
while !stack.is_empty() && *stack.last().unwrap() == popped[pop_idx] {
27+
stack.pop();
28+
pop_idx += 1;
29+
}
30+
}
31+
stack.is_empty()
32+
}
33+
}
34+
35+
// Tests.
36+
fn main() {
37+
let tests = [
38+
([1, 2, 3, 4, 5], [4, 5, 3, 2, 1], true),
39+
([1, 2, 3, 4, 5], [4, 3, 5, 1, 2], false),
40+
];
41+
for t in tests {
42+
assert_eq!(
43+
Solution::validate_stack_sequences(Vec::from(t.0), Vec::from(t.1)),
44+
t.2
45+
);
46+
}
47+
println!("\x1b[92m» All tests passed!\x1b[0m")
48+
}

0 commit comments

Comments
 (0)