-
Notifications
You must be signed in to change notification settings - Fork 269
add: rotate image solution and testing suite #192 #193
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
src/_Problems_/rotate-image/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.