-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiagonal-traverse-ii.rs
107 lines (103 loc) · 3.65 KB
/
diagonal-traverse-ii.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 1424. Diagonal Traverse II
// 🟠 Medium
//
// https://leetcode.com/problems/diagonal-traverse-ii/
//
// Tags: Array - Sorting - Heap (Priority Queue)
use std::collections::VecDeque;
struct Solution;
impl Solution {
/// If we reverse the rows in the input and add them one by one popping the first element and
/// adding it to the result before we add it to a rotative queue that points to the row from
/// which the next element needs to come, we can keep popping the last element of the vector at
/// which that queue points and adding them to the result.
///
/// Time complexity: O(m*n) - We visit each element in the input once.
/// Space complexity: O(m*n) - We make a local copy of the input 2D vector.
///
/// Runtime 36 ms Beats 75%
/// Memory 5.54 MB Beats 100%
pub fn find_diagonal_order(nums: Vec<Vec<i32>>) -> Vec<i32> {
let n = nums.len();
let mut nums = nums
.into_iter()
.map(|mut v| {
v.reverse();
v
})
.collect::<Vec<_>>();
// A vector with the indexes of the next vector from nums from which we have to pop.
let mut idxs: VecDeque<usize> = VecDeque::new();
// A pointer to the next vector in nums that we have not used yet.
let mut next_idx = 0;
let mut res = vec![];
while next_idx < n || !idxs.is_empty() {
if next_idx < n {
// Each row is guaranteed to have, at least, one element.
res.push(nums[next_idx].pop().unwrap());
}
// Now pop from all rows that are not empty.
for _ in 0..idxs.len() {
let i = idxs.pop_front().unwrap();
res.push(nums[i].pop().unwrap());
if !nums[i].is_empty() {
idxs.push_back(i);
}
}
if next_idx < n {
if !nums[next_idx].is_empty() {
idxs.push_front(next_idx);
}
next_idx += 1;
}
}
res
}
/// Use a BFS approach, start at the top-left corner, when we visit a cell, add its neighbors
/// to the queue and its value to the result. Simpler approach than the previous one.
///
/// Time complexity: O(m*n) - We visit each element in the input once.
/// Space complexity: O(√n) - The queue can grow to the size of one diagonal.
///
/// Runtime 28 ms Beats 100%
/// Memory 5.54 MB Beats 100%
pub fn find_diagonal_order_bfs(nums: Vec<Vec<i32>>) -> Vec<i32> {
let n = nums.len() - 1;
let mut q = VecDeque::from([(0, 0)]);
let mut res = vec![];
while let Some((row, col)) = q.pop_front() {
res.push(nums[row][col]);
if col == 0 && row < n {
q.push_back((row + 1, 0));
}
if col + 1 < nums[row].len() {
q.push_back((row, col + 1));
}
}
res
}
}
// Tests.
fn main() {
let tests = [
(
vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]],
vec![1, 4, 2, 7, 5, 3, 8, 6, 9],
),
(
vec![
vec![1, 2, 3, 4, 5],
vec![6, 7],
vec![8],
vec![9, 10, 11],
vec![12, 13, 14, 15, 16],
],
vec![1, 6, 2, 8, 7, 3, 9, 4, 12, 10, 5, 13, 11, 14, 15, 16],
),
];
for t in tests {
assert_eq!(Solution::find_diagonal_order(t.0.clone()), t.1);
assert_eq!(Solution::find_diagonal_order_bfs(t.0.clone()), t.1);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}