Skip to content

Commit 451d5ed

Browse files
authored
Merge pull request #3521 from aadil42/patch-71
Create 2125-number-of-laser-beams-in-a-bank.js
2 parents 26d7fb0 + be683f2 commit 451d5ed

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: javascript/2125-number-of-laser-beams-in-a-bank.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Two Pointers | Math | Array
3+
* Time O(n) | Space O(1)
4+
* https://leetcode.com/problems/number-of-laser-beams-in-a-bank/
5+
* @param {string[]} bank
6+
* @return {number}
7+
*/
8+
var numberOfBeams = function(bank) {
9+
10+
let totalBeams = 0;
11+
12+
let left = 0;
13+
let right = left + 1;
14+
15+
const countBeam = (beam) => {
16+
return beam.split("").filter((b) => b === "1").length;
17+
}
18+
19+
while (right < bank.length) {
20+
while (right < bank.length && !countBeam(bank[right])) {
21+
right++;
22+
}
23+
24+
if (right < bank.length) {
25+
totalBeams += countBeam(bank[left]) * countBeam(bank[right]);
26+
}
27+
left = right;
28+
right++;
29+
}
30+
return totalBeams;
31+
};

0 commit comments

Comments
 (0)