LeetCode Entry

2125. Number of Laser Beams in a Bank

27.10.2025 medium 2025 kotlin rust

Count multiplications between rows

2125. Number of Laser Beams in a Bank medium blog post substack youtube

5a71695d-3b4c-4c0b-bbf8-0c85a67b717f (1).webp

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()
    }