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.
1 parent a5b00e6 commit 4a5e9a3Copy full SHA for 4a5e9a3
kotlin/2125-number-of-laser-beams-in-a-bank.kt
@@ -0,0 +1,25 @@
1
+class Solution {
2
+ fun numberOfBeams(bank: Array<String>): Int {
3
+ var prev = bank[0].count { it == '1'}
4
+ var res = 0
5
+
6
+ for (i in 1 until bank.size) {
7
+ val cur = bank[i].count { it == '1'}
8
+ if (cur > 0) {
9
+ res += (prev * cur)
10
+ prev = cur
11
+ }
12
13
14
+ return res
15
16
+}
17
18
+// or do it the kotlin way!
19
20
+ fun numberOfBeams(bank: Array<String>) = bank
21
+ .map { it.count { it == '1' } }
22
+ .filterNot { it == 0 }
23
+ .zipWithNext { a, b -> a * b }
24
+ .sum()
25
0 commit comments