Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: rotate image solution and testing suite #192 #193

Merged
merged 1 commit into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
83 changes: 83 additions & 0 deletions src/_Problems_/rotate-image/index.js
Original file line number Diff line number Diff line change
@@ -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 };
58 changes: 58 additions & 0 deletions src/_Problems_/rotate-image/rotate-image.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});