Skip to content

Commit d1abfb1

Browse files
committed
LC 1861. Rotating the Box (Rust)
1 parent 03ccac3 commit d1abfb1

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ to the solution in this repository.
629629
| [1846. Maximum Element After Decreasing and Rearranging][lc1846] | 🟠 Medium | [![rust](res/rs.png)][lc1846rs] |
630630
| [1854. Maximum Population Year][lc1854] | 🟢 Easy | [![python](res/py.png)][lc1854py] |
631631
| [1857. Largest Color Value in a Directed Graph][lc1857] | 🔴 Hard | [![python](res/py.png)][lc1857py] |
632+
| [1861. Rotating the Box][lc1861] | 🟠 Medium | [![rust](res/rs.png)][lc1861rs] |
632633
| [1863. Sum of All Subset XOR Totals][lc1863] | 🟢 Easy | [![rust](res/rs.png)][lc1863rs] |
633634
| [1877. Minimize Maximum Pair Sum in Array][lc1877] | 🟠 Medium | [![rust](res/rs.png)][lc1877rs] |
634635
| [1887. Reduction Operations to Make the Array Elements Equal][lc1887] | 🟠 Medium | [![rust](res/rs.png)][lc1887rs] |
@@ -2182,6 +2183,8 @@ to the solution in this repository.
21822183
[lc1854py]: leetcode/maximum-population-year.py
21832184
[lc1857]: https://leetcode.com/problems/largest-color-value-in-a-directed-graph/
21842185
[lc1857py]: leetcode/largest-color-value-in-a-directed-graph.py
2186+
[lc1861]: https://leetcode.com/problems/rotating-the-box/
2187+
[lc1861rs]: leetcode/rotating-the-box.rs
21852188
[lc1863]: https://leetcode.com/problems/sum-of-all-subset-xor-totals/
21862189
[lc1863rs]: leetcode/sum-of-all-subset-xor-totals.rs
21872190
[lc1877]: https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/

leetcode/rotating-the-box.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// 1861. Rotating the Box
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/rotating-the-box/
5+
//
6+
// Tags: Array - Two Pointers - Matrix
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Create the result matrix, iterate over the input by row and reversed column using a read
11+
/// and write pointer, when we find a rock, write it to the current position of the write
12+
/// pointer and shift it one row up, when we find an obstacle, write it to its rotated position
13+
/// and move the write pointer to the cell above it.
14+
///
15+
/// Time complexity: O(m*n) - We iterate over m rows and n columns.
16+
/// Space complexity: O(m*n) - The result matrix, if we take it into account.
17+
///
18+
/// Runtime 138 ms Beats 83%
19+
/// Memory 18.87 MB Beats 50%
20+
pub fn rotate_the_box(matrix: Vec<Vec<char>>) -> Vec<Vec<char>> {
21+
// The number of rows and columns in the input matrix.
22+
let (num_rows, num_cols) = (matrix.len(), matrix[0].len());
23+
let mut res = vec![vec!['.'; num_rows]; num_cols];
24+
let (mut write_row, mut write_col);
25+
for row in 0..num_rows {
26+
write_col = num_rows - row - 1;
27+
write_row = num_cols - 1;
28+
for col in (0..num_cols).rev() {
29+
match matrix[row][col] {
30+
'*' => {
31+
// Obstacle, it will not move and block the current cell.
32+
res[col][write_col] = '*';
33+
if col > 0 {
34+
write_row = col - 1;
35+
}
36+
}
37+
'#' => {
38+
// Rock, it will fall as far as it can and block that cell.
39+
res[write_row][write_col] = '#';
40+
if write_row > 0 {
41+
write_row -= 1;
42+
}
43+
}
44+
_ => (),
45+
}
46+
}
47+
}
48+
res
49+
}
50+
}
51+
52+
// Tests.
53+
fn main() {
54+
let tests = [
55+
(
56+
vec![vec!['#', '.', '#']],
57+
vec![vec!['.'], vec!['#'], vec!['#']],
58+
),
59+
(
60+
vec![vec!['#', '.', '*', '.'], vec!['#', '#', '*', '.']],
61+
vec![
62+
vec!['#', '.'],
63+
vec!['#', '#'],
64+
vec!['*', '*'],
65+
vec!['.', '.'],
66+
],
67+
),
68+
(
69+
vec![
70+
vec!['#', '#', '*', '.', '*', '.'],
71+
vec!['#', '#', '#', '*', '.', '.'],
72+
vec!['#', '#', '#', '.', '#', '.'],
73+
],
74+
vec![
75+
vec!['.', '#', '#'],
76+
vec!['.', '#', '#'],
77+
vec!['#', '#', '*'],
78+
vec!['#', '*', '.'],
79+
vec!['#', '.', '*'],
80+
vec!['#', '.', '.'],
81+
],
82+
),
83+
];
84+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
85+
let mut success = 0;
86+
for (i, t) in tests.iter().enumerate() {
87+
let res = Solution::rotate_the_box(t.0.clone());
88+
if res == t.1 {
89+
success += 1;
90+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
91+
} else {
92+
println!(
93+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {:?}!!\x1b[0m",
94+
i, t.1, res
95+
);
96+
}
97+
}
98+
println!();
99+
if success == tests.len() {
100+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
101+
} else if success == 0 {
102+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
103+
} else {
104+
println!(
105+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
106+
tests.len() - success
107+
)
108+
}
109+
}

0 commit comments

Comments
 (0)