Skip to content

Instantly share code, notes, and snippets.

@IEdiong
Created April 18, 2022 20:50
Show Gist options
  • Select an option

  • Save IEdiong/f18d0d14f2f9516cb6209d0f92ea03e4 to your computer and use it in GitHub Desktop.

Select an option

Save IEdiong/f18d0d14f2f9516cb6209d0f92ea03e4 to your computer and use it in GitHub Desktop.

Revisions

  1. IEdiong created this gist Apr 18, 2022.
    29 changes: 29 additions & 0 deletions matrixElementsSum.js
    Original 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;
    }