|
| 1 | +// Rotate Matrix: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method |
| 2 | +// to rotate the image by 90 degrees. Can you do this in place? |
| 3 | + |
| 4 | +const assert = require("assert"); |
| 5 | + |
| 6 | +/** |
| 7 | + * Rotates the inputted 2d array by 90 degrees |
| 8 | + * @param {Array.<number[]>} matrix input 2d array to rotate |
| 9 | + * @return {null} |
| 10 | + * |
| 11 | + * rotateMatrix is a void function since we don't actually return anything, but rather mutate the input array. |
| 12 | + * The trick here is to take the same knowledge of swapping two elements in an array with a temp to do it in place, |
| 13 | + * |
| 14 | + * const swap = (arr, i, j) => { |
| 15 | + * const temp = arr[i]; |
| 16 | + * arr[i] = arr[j]; |
| 17 | + * arr[j] = temp; |
| 18 | + * } |
| 19 | + * |
| 20 | + * and apply that to a 2d array. Instead of tracking i and j in one array, we keep track of i, j, M-1-i, and M-1-j. |
| 21 | + * As we iterate through the array, the positions that we swap shift by one until we finish rotating elements, layer by |
| 22 | + * layer. |
| 23 | + * |
| 24 | + * Runtime: O(N^2) |
| 25 | + * Space: O(1) |
| 26 | + * |
| 27 | + */ |
| 28 | +const rotateMatrix = (matrix) => { |
| 29 | + if (matrix.length < 2) return; |
| 30 | + |
| 31 | + const M = matrix.length; |
| 32 | + for (let i = 0; i < M / 2; i++) { |
| 33 | + for (let j = i; j < M - i - 1; j++) { |
| 34 | + const temp = matrix[i][j]; |
| 35 | + // swap i, j, M-1-i, M-1-j |
| 36 | + matrix[i][j] = matrix[j][M - 1 - i]; |
| 37 | + matrix[j][M - 1 - i] = matrix[M - 1 - i][M - 1 - j]; |
| 38 | + matrix[M - 1 - i][M - 1 - j] = matrix[M - 1 - j][i]; |
| 39 | + matrix[M - 1 - j][i] = temp; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return; |
| 44 | +}; |
| 45 | + |
| 46 | +describe(module.filename, () => { |
| 47 | + it("should not affect a matrix of size 0", () => { |
| 48 | + const matrix = []; |
| 49 | + |
| 50 | + rotateMatrix(matrix); |
| 51 | + assert.deepEqual(matrix, []); |
| 52 | + }); |
| 53 | + it("should not affect a matrix of size 1", () => { |
| 54 | + const matrix = [5]; |
| 55 | + |
| 56 | + rotateMatrix(matrix); |
| 57 | + assert.deepEqual(matrix, [5]); |
| 58 | + }); |
| 59 | + it("should rotate a 2x2 matrix 90 degrees", () => { |
| 60 | + const matrix = [ |
| 61 | + [1, 2], |
| 62 | + [3, 4], |
| 63 | + ]; |
| 64 | + |
| 65 | + rotateMatrix(matrix); |
| 66 | + assert.deepEqual(matrix, [ |
| 67 | + [2, 4], |
| 68 | + [1, 3], |
| 69 | + ]); |
| 70 | + }); |
| 71 | + it("should rotate a 3x3 matrix 90 degrees", () => { |
| 72 | + const matrix = [ |
| 73 | + [1, 2, 3], |
| 74 | + [4, 5, 6], |
| 75 | + [7, 8, 9], |
| 76 | + ]; |
| 77 | + |
| 78 | + rotateMatrix(matrix); |
| 79 | + assert.deepEqual(matrix, [ |
| 80 | + [3, 6, 9], |
| 81 | + [2, 5, 8], |
| 82 | + [1, 4, 7], |
| 83 | + ]); |
| 84 | + }); |
| 85 | + it("should rotate a 4x4 matrix 90 degrees", () => { |
| 86 | + const matrix = [ |
| 87 | + [11, 12, 13, 14], |
| 88 | + [15, 16, 17, 18], |
| 89 | + [19, 20, 21, 22], |
| 90 | + [23, 24, 25, 26], |
| 91 | + ]; |
| 92 | + |
| 93 | + rotateMatrix(matrix); |
| 94 | + assert.deepEqual(matrix, [ |
| 95 | + [14, 18, 22, 26], |
| 96 | + [13, 17, 21, 25], |
| 97 | + [12, 16, 20, 24], |
| 98 | + [11, 15, 19, 23], |
| 99 | + ]); |
| 100 | + }); |
| 101 | + it("should bring the matrix to its original position after rotating 4 times.", () => { |
| 102 | + const matrix = [ |
| 103 | + [1, 2, 3], |
| 104 | + [4, 5, 6], |
| 105 | + [7, 8, 9], |
| 106 | + ]; |
| 107 | + |
| 108 | + rotateMatrix(matrix); |
| 109 | + rotateMatrix(matrix); |
| 110 | + rotateMatrix(matrix); |
| 111 | + rotateMatrix(matrix); |
| 112 | + assert.deepEqual(matrix, [ |
| 113 | + [1, 2, 3], |
| 114 | + [4, 5, 6], |
| 115 | + [7, 8, 9], |
| 116 | + ]); |
| 117 | + }); |
| 118 | +}); |
0 commit comments