Created
April 18, 2022 20:50
-
-
Save IEdiong/f18d0d14f2f9516cb6209d0f92ea03e4 to your computer and use it in GitHub Desktop.
Code Signal matrixElementsSum Solution in JS
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
function solution(matrix) { | |
const hauntedRoom = {}; | |
const numberOfFloors = matrix.length; | |
const numberOfRooms = matrix[0].length; | |
let total = 0; | |
// 1. Loop through each row | |
for (let floor = 0; floor < numberOfFloors; floor++) { | |
// 2. For each element in the row | |
for (let room = 0; room < numberOfRooms; room++) { | |
if (matrix[floor][room] === 0) { | |
if (room in hauntedRoom) { | |
hauntedRoom[room]++; | |
} else { | |
hauntedRoom[room] = 1; | |
} | |
} else { | |
if (!(room in hauntedRoom)) { | |
total += matrix[floor][room]; | |
} | |
} | |
} | |
} | |
// 3. If the column is a no no skip | |
// 4. If the element is haunted and not already noted add to the no no group | |
// 5. Else add the cost to the total | |
// 6. Return the total | |
return total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment