Created
April 18, 2022 20:50
-
-
Save IEdiong/f18d0d14f2f9516cb6209d0f92ea03e4 to your computer and use it in GitHub Desktop.
Revisions
-
IEdiong created this gist
Apr 18, 2022 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ 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; }