From cdc2ca10b460e4d38bb605bfe4cd64ac26a24935 Mon Sep 17 00:00:00 2001 From: Ignacio Herrera Date: Wed, 3 Jan 2024 19:46:56 -0300 Subject: [PATCH] Leetcode/2125 --- README.md | 1 + problems/leetcode/2125/solution.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 problems/leetcode/2125/solution.js diff --git a/README.md b/README.md index 62451b7..fcf42ed 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/problems/leetcode/2125/solution.js b/problems/leetcode/2125/solution.js new file mode 100644 index 0000000..3f0ca7d --- /dev/null +++ b/problems/leetcode/2125/solution.js @@ -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); +}