Created
February 19, 2024 22:28
-
-
Save blentz100/507976f0206c4aca889d2d8a637f6ed1 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview Matrix Problem
This file contains 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
const inputMatrix = [ | |
[1, 1, 4, 1], | |
[4, 3, 3, 6], | |
[5, 3, 4, 6], | |
[9, 3, 5, 5], | |
]; | |
const outputMatrix = [ | |
[9, 5, 4, 1], | |
[3, 3, 3, 1], | |
[5, 4, 3, 4], | |
[5, 6, 6, 1], | |
]; | |
function rotateMatrix (inputMatrix){ | |
const solutionMatrix= []; | |
let tempArray = []; | |
for(let j = 0; j < inputMatrix.length; j++){ | |
tempArray = []; | |
for(let i=inputMatrix.length -1; i >= 0; i--){ | |
tempArray.push(inputMatrix[i][j]) | |
} | |
solutionMatrix.push(tempArray) | |
} | |
return solutionMatrix; | |
} | |
console.log(rotateMatrix(inputMatrix)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment