From 6643c73ae4793614731895c465edecad01bcc805 Mon Sep 17 00:00:00 2001 From: Ryan McPherson <71467277+RyanMcPherson7@users.noreply.github.com> Date: Sun, 30 Jan 2022 13:42:38 -0500 Subject: [PATCH] add: rotate image problem, solution, and testing suite (#193) Merging, thanks. --- TOC.md | 1 + src/_Problems_/rotate-image/index.js | 83 +++++++++++++++++++ .../rotate-image/rotate-image.test.js | 58 +++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 src/_Problems_/rotate-image/index.js create mode 100644 src/_Problems_/rotate-image/rotate-image.test.js diff --git a/TOC.md b/TOC.md index 086d1ab6..7765ff77 100644 --- a/TOC.md +++ b/TOC.md @@ -75,6 +75,7 @@ - [Maximum Product of Three Numbers](src/_Problems_/max-product-of-3-numbers) - [Next Greater for Every Element in an Array](src/_Problems_/next-greater-element) - [Compose Largest Number](src/_Problems_/compose-largest-number) +- [Rotate Image](src/_Problems_/rotate-image) ### Searching diff --git a/src/_Problems_/rotate-image/index.js b/src/_Problems_/rotate-image/index.js new file mode 100644 index 00000000..3297416d --- /dev/null +++ b/src/_Problems_/rotate-image/index.js @@ -0,0 +1,83 @@ +/* + Rotate Image (LeetCode #48) + + - You are given an n x n 2D matrix representing an image, rotate the + image by 90 degrees (clockwise). + - You have to rotate the image in-place, which means you have to modify + the input 2D matrix directly. DO NOT allocate another 2D matrix and do + the rotation. + + Example 1: + + 1 2 3 7 4 1 + 4 5 6 ---> 8 5 2 + 7 8 9 9 6 3 + + - Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] + - Output: [[7,4,1],[8,5,2],[9,6,3]] + + Example 2: + + 5 1 9 11 15 13 2 5 + 2 4 8 10 ---> 14 3 4 1 + 13 3 6 7 12 6 8 9 + 15 14 12 16 16 7 10 11 + + - Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] + - Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] + + Constraints: + - n == matrix.length == matrix[i].length + - 1 <= n <= 20 + - -1000 <= matrix[i][j] <= 1000 +*/ + +/* + Solution: + + 1. First, take the transpose of the matrix + - Example: + 1 2 3 1 4 7 + 4 5 6 ---> 2 5 8 + 7 8 9 3 6 9 + + 2. Second, flip the matrix horizontally + - Example: + 1 4 7 7 4 1 + 2 5 8 ---> 8 5 2 + 3 6 9 9 6 3 + + 3. Problem solved! + + - Solution is O(n) where n is the size (total number of entries) of the + input matrix +*/ + +function rotateImage(matrix) { + const n = matrix.length; + + // take transpose + for (let i = 0; i < n; i++) { + for (let j = i; j < n; j++) { + const temp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = temp; + } + } + + // flip horizontally + for (let i = 0; i < n; i++) { + let left = 0; + let right = n - 1; + + while (left < right) { + const temp = matrix[i][left]; + matrix[i][left] = matrix[i][right]; + matrix[i][right] = temp; + left++; + right--; + } + } +} + +module.exports = { rotateImage }; diff --git a/src/_Problems_/rotate-image/rotate-image.test.js b/src/_Problems_/rotate-image/rotate-image.test.js new file mode 100644 index 00000000..95b44bf7 --- /dev/null +++ b/src/_Problems_/rotate-image/rotate-image.test.js @@ -0,0 +1,58 @@ +const { rotateImage } = require('.'); + +describe('Rotate Image', () => { + it('Should rotate 3x3 matrix elements by 90 degrees', () => { + const inputMatrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ]; + + const expectedMatrix = [ + [7, 4, 1], + [8, 5, 2], + [9, 6, 3], + ]; + + rotateImage(inputMatrix); + expect(inputMatrix).toEqual(expectedMatrix); + }); + it('Should rotate 4x4 matrix elements by 90 degrees', () => { + const inputMatrix = [ + [5, 1, 9, 11], + [2, 4, 8, 10], + [13, 3, 6, 7], + [15, 14, 12, 16], + ]; + + const expectedMatrix = [ + [15, 13, 2, 5], + [14, 3, 4, 1], + [12, 6, 8, 9], + [16, 7, 10, 11], + ]; + + rotateImage(inputMatrix); + expect(inputMatrix).toEqual(expectedMatrix); + }); + it('Should rotate 5x5 matrix elements by 90 degrees', () => { + const inputMatrix = [ + [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], + ]; + + const expectedMatrix = [ + [21, 16, 11, 6, 1], + [22, 17, 12, 7, 2], + [23, 18, 13, 8, 3], + [24, 19, 14, 9, 4], + [25, 20, 15, 10, 5], + ]; + + rotateImage(inputMatrix); + expect(inputMatrix).toEqual(expectedMatrix); + }); +});