We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 26d7fb0 + be683f2 commit 451d5edCopy full SHA for 451d5ed
javascript/2125-number-of-laser-beams-in-a-bank.js
@@ -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
29
30
+ return totalBeams;
31
+};
0 commit comments