Skip to content

Instantly share code, notes, and snippets.

@blentz100
Created February 19, 2024 22:28
Show Gist options
  • Save blentz100/507976f0206c4aca889d2d8a637f6ed1 to your computer and use it in GitHub Desktop.
Save blentz100/507976f0206c4aca889d2d8a637f6ed1 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview Matrix Problem
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