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
108
109
|
/// 1861. Rotating the Box (Medium)
///
/// You are given an m x n matrix of characters box representing a side-view of
/// a box. Each cell of the box is one of the following:
///
/// * A stone '#'
/// * A stationary obstacle '*'
/// * Empty '.'
///
/// The box is rotated *90 degrees clockwise*, causing some of the stones to
/// fall due to gravity. Each stone falls down until it lands on an obstacle,
/// another stone, or the bottom of the box. Gravity *does not* affect the
/// obstacles' positions, and the inertia from the box's rotation *does not
/// *affect the stones' horizontal positions.
///
/// It is *guaranteed* that each stone in box rests on an obstacle, another
/// stone, or the bottom of the box.
///
/// Return an n x m matrix representing the box after the rotation described
/// above.
///
/// *Example 1:*
///
/// *Input:* box = [["#",".","#"]]
/// *Output:* [["."],
/// ["#"],
/// ["#"]]
///
/// *Example 2:*
///
/// *Input:* box = [["#",".","*","."],
/// ["#","#","*","."]]
/// *Output:* [["#","."],
/// ["#","#"],
/// ["*","*"],
/// [".","."]]
///
/// *Example 3:*
///
/// *Input:* box = [["#","#","*",".","*","."],
/// ["#","#","#","*",".","."],
/// ["#","#","#",".","#","."]]
/// *Output:* [[".","#","#"],
/// [".","#","#"],
/// ["#","#","*"],
/// ["#","*","."],
/// ["#",".","*"],
/// ["#",".","."]]
///
/// *Constraints:*
///
/// * m == box.length
/// * n == box[i].length
/// * 1 <= m, n <= 500
/// * box[i][j] is either '#', '*', or '.'.
///
use leetcode::*;
struct Solution {}
impl Solution {
pub fn rotate_the_box(in_box: Vec<Vec<char>>) -> Vec<Vec<char>> {
let n = in_box.len();
let m = in_box[0].len();
let mut out_box = vec![vec!['.'; n]; m];
for x in (0..n).rev() {
let mut last = m - 1;
for y in (0..m).rev() {
let c = in_box[n - 1 - x][y];
if c == '*' {
out_box[y][x] = '*';
if y > 0 {
last = y - 1;
}
} else if c == '#' {
out_box[last][x] = '#';
last = last.saturating_sub(1);
}
}
}
out_box
}
}
pub fn main() {
println!("{:?}", Solution::rotate_the_box(arg_into(1)));
}
#[cfg(test)]
mod tests {
use crate::Solution;
use leetcode::vvc;
#[test]
fn test() {
assert_eq!(
Solution::rotate_the_box(vvc("[[#,.,#]]")),
vvc("[[.],[#],[#]]")
);
assert_eq!(
Solution::rotate_the_box(vvc("[[#,.,*,.],[#,#,*,.]]")),
vvc("[[#,.],[#,#],[*,*],[.,.]]")
);
assert_eq!(
Solution::rotate_the_box(vvc("[[#,#,*,.,*,.],[#,#,#,*,.,.],[#,#,#,.,#,.]]")),
vvc("[[.,#,#],[.,#,#],[#,#,*],[#,*,.],[#,.,*],[#,.,.]]")
)
}
}
|