Skip to content

Commit

Permalink
Leetcode/2125
Browse files Browse the repository at this point in the history
  • Loading branch information
herrera-ignacio committed Jan 3, 2024
1 parent d5374f2 commit cdc2ca1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
- [1832: Check if the sentence is pangram](problems/leetcode/1832)
- [1926: Nearest exit from entrance in maze](problems/leetcode/1926)
- [2095: Delete the middle node of a linked list](problems/leetcode/2095)
- [2125: Number of laser beams in a bank](problems/leetcode/2125)
- [2131: Longest palindrome by concatenating two letter words](problems/leetcode/2131)
- [2225: Find players with zero or one losses](problems/leetcode/2225) - Map
- [2610: Convert an array into a 2d array with conditions](problems/leetcode/2610) - Medium - Array
29 changes: 29 additions & 0 deletions problems/leetcode/2125/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const isSecurityDevice = (str) => str === '1';

/**
*
* @param {string[]} bank
* @return {number}
*/
const numberOfBeams = (bank) => {
const rowsWithSecurityDevices = [];

for (let i = 0; i < bank.length; i++) {
let totalDevices = 0;
for (let j = 0; j < bank[i].length; j++) {
if (isSecurityDevice(bank[i][j])) {
totalDevices++;
}
}

if (totalDevices > 0) {
rowsWithSecurityDevices.push(totalDevices);
}
}

return rowsWithSecurityDevices.reduce((acc, devices, i) => {
return i + 1 < rowsWithSecurityDevices.length
? acc + devices * rowsWithSecurityDevices[i + 1]
: acc
}, 0);
}

0 comments on commit cdc2ca1

Please sign in to comment.