|
| 1 | +/** |
| 2 | + * [1914] Cyclically Rotating a Grid |
| 3 | + * |
| 4 | + * You are given an m x n integer matrix grid, where m and n are both even integers, and an integer k. |
| 5 | + * |
| 6 | + * The matrix is composed of several layers, which is shown in the below image, where each color is its own layer: |
| 7 | + * |
| 8 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid.png" style="width: 231px; height: 258px;" /> |
| 9 | + * |
| 10 | + * A cyclic rotation of the matrix is done by cyclically rotating each layer in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the counter-clockwise direction. An example rotation is shown below: |
| 11 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/explanation_grid.jpg" style="width: 500px; height: 268px;" /> |
| 12 | + * Return the matrix after applying k cyclic rotations to it. |
| 13 | + * |
| 14 | + * |
| 15 | + * Example 1: |
| 16 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/rod2.png" style="width: 421px; height: 191px;" /> |
| 17 | + * |
| 18 | + * Input: grid = [[40,10],[30,20]], k = 1 |
| 19 | + * Output: [[10,20],[40,30]] |
| 20 | + * Explanation: The figures above represent the grid at every state. |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid5.png" style="width: 231px; height: 262px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid6.png" style="width: 231px; height: 262px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid7.png" style="width: 231px; height: 262px;" /> |
| 25 | + * |
| 26 | + * |
| 27 | + * Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2 |
| 28 | + * Output: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]] |
| 29 | + * Explanation: The figures above represent the grid at every state. |
| 30 | + * |
| 31 | + * |
| 32 | + * |
| 33 | + * Constraints: |
| 34 | + * |
| 35 | + * |
| 36 | + * m == grid.length |
| 37 | + * n == grid[i].length |
| 38 | + * 2 <= m, n <= 50 |
| 39 | + * Both m and n are even integers. |
| 40 | + * 1 <= grid[i][j] <=^ 5000 |
| 41 | + * 1 <= k <= 10^9 |
| 42 | + * |
| 43 | + */ |
| 44 | +pub struct Solution {} |
| 45 | + |
| 46 | +// problem: https://leetcode.com/problems/cyclically-rotating-a-grid/ |
| 47 | +// discuss: https://leetcode.com/problems/cyclically-rotating-a-grid/discuss/?currentPage=1&orderBy=most_votes&query= |
| 48 | + |
| 49 | +// submission codes start here |
| 50 | + |
| 51 | +impl Solution { |
| 52 | + pub fn rotate_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> { |
| 53 | + let k = k as usize; |
| 54 | + let mut grid = grid; |
| 55 | + |
| 56 | + let m = grid.len(); |
| 57 | + let n = grid[0].len(); |
| 58 | + |
| 59 | + let num_rings = m.min(n) / 2; |
| 60 | + |
| 61 | + for i in 0..num_rings { |
| 62 | + let num_rotations = k % (2 * (m + n - 4 * i) - 4); |
| 63 | + for _ in 0..num_rotations { |
| 64 | + for j in i..n - i - 1 { |
| 65 | + grid[i].swap(j, j + 1); |
| 66 | + } |
| 67 | + |
| 68 | + for j in i..m - i - 1 { |
| 69 | + let tmp = grid[j][n - i - 1]; |
| 70 | + grid[j][n - i - 1] = grid[j + 1][n - i - 1]; |
| 71 | + grid[j + 1][n - i - 1] = tmp; |
| 72 | + } |
| 73 | + |
| 74 | + let mut j = n - i - 1; |
| 75 | + |
| 76 | + while j > i { |
| 77 | + grid[m - i - 1].swap(j, j - 1); |
| 78 | + j -= 1; |
| 79 | + } |
| 80 | + |
| 81 | + let mut j = m - i - 1; |
| 82 | + |
| 83 | + while j > i + 1 { |
| 84 | + let tmp = grid[j][i]; |
| 85 | + grid[j][i] = grid[j - 1][i]; |
| 86 | + grid[j - 1][i] = tmp; |
| 87 | + j -= 1; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + grid |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// submission codes end |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_1914_example_1() { |
| 104 | + let grid = vec![vec![40, 10], vec![30, 20]]; |
| 105 | + let k = 1; |
| 106 | + |
| 107 | + let result = vec![vec![10, 20], vec![40, 30]]; |
| 108 | + |
| 109 | + assert_eq!(Solution::rotate_grid(grid, k), result); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_1914_example_2() { |
| 114 | + let grid = vec![ |
| 115 | + vec![1, 2, 3, 4], |
| 116 | + vec![5, 6, 7, 8], |
| 117 | + vec![9, 10, 11, 12], |
| 118 | + vec![13, 14, 15, 16], |
| 119 | + ]; |
| 120 | + let k = 2; |
| 121 | + |
| 122 | + let result = vec![ |
| 123 | + vec![3, 4, 8, 12], |
| 124 | + vec![2, 11, 10, 16], |
| 125 | + vec![1, 7, 6, 15], |
| 126 | + vec![5, 9, 13, 14], |
| 127 | + ]; |
| 128 | + |
| 129 | + assert_eq!(Solution::rotate_grid(grid, k), result); |
| 130 | + } |
| 131 | +} |
0 commit comments