-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspiral-matrix-ii.rs
99 lines (96 loc) · 2.8 KB
/
spiral-matrix-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
// 59. Spiral Matrix II
// 🟠 Medium
//
// https://leetcode.com/problems/spiral-matrix-ii/
//
// Tags: Array - Matrix - Simulation
enum State {
Right,
Down,
Left,
Up,
}
struct Solution;
impl Solution {
/// Use a state machine with four states, corresponding to the four
/// directions we can travel. Depending on the state, use current row and
/// column values and unvisited row and column values to determine if we
/// need to update or preserve the state.
///
/// Time complexity: O(n^2) - We do O(1) work, adjusting state and some
/// usize and i32 values, for each value between 1 and n^2.
/// Space complexity: O(1) - If we do not take into account the output
/// matrix, otherwise O(n^2).
///
/// Runtime 0 ms Beats 100%
/// Memory 2 MB Beats 96.42%
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
let n = n as usize;
let mut matrix = vec![vec![0; n]; n];
let mut state = State::Right;
let (mut r, mut c) = (0, 0);
let (mut top, mut right, mut bottom, mut left) = (0, n - 1, n - 1, 0);
for i in 1..=(n * n) {
matrix[r][c] = i as i32;
match state {
State::Right => {
if c == right {
top += 1;
state = State::Down;
r += 1;
} else {
c += 1;
}
}
State::Down => {
if r == bottom {
right = right - 1;
state = State::Left;
c -= 1;
} else {
r += 1;
}
}
State::Left => {
if c == left {
bottom -= 1;
state = State::Up;
r -= 1;
} else {
c -= 1;
}
}
State::Up => {
if r == top {
left += 1;
state = State::Right;
c += 1;
} else {
r -= 1;
}
}
}
}
matrix
}
}
// Tests.
fn main() {
let tests = [
(1, vec![vec![1]]),
(3, vec![vec![1, 2, 3], vec![8, 9, 4], vec![7, 6, 5]]),
(
4,
vec![
vec![1, 2, 3, 4],
vec![12, 13, 14, 5],
vec![11, 16, 15, 6],
vec![10, 9, 8, 7],
],
),
];
for t in tests {
assert_eq!(Solution::generate_matrix(t.0), t.1);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}