LeetCode Entry
2125. Number of Laser Beams in a Bank
Count multiplications between rows
2125. Number of Laser Beams in a Bank medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1155
Problem TLDR
Count multiplications between rows #medium
Intuition
Total += previous * current (count ‘1’)
Approach
- empty rows are irrelevant
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\) or O(1)
Code
// 56ms
fun numberOfBeams(b: Array<String>) = b
.mapNotNull { it.sumOf { it - '0' }.takeIf { it > 0 }}
.windowed(2).sumOf { it[0]*it[1] }
// 0ms
pub fn number_of_beams(b: Vec<String>) -> i32 {
b.iter().map(|s| s.bytes().filter(|&b| b == b'1').count())
.filter(|&c| c > 0).tuple_windows().map(|(a,b)| (a*b) as i32).sum()
}