|
| 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