LeetCode Entry

1582. Special Positions in a Binary Matrix

04.08.2026 easy 2026 kotlin rust

Count single 1-row-column

1582. Special Positions in a Binary Matrix easy blog post substack youtube

7e6d1320-c887-4b63-976e-83ef9189f37b (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1287

Problem TLDR

Count single 1-row-column #easy

Intuition

Brute-force.

Approach

  • check each cell
  • or check each row

Complexity

  • Time complexity: \(O(n^2m^2)\), can be O(nm)

  • Space complexity: \(O(1)\)

Code

// 14ms
    fun numSpecial(m: Array<IntArray>) = m.count { r ->
        r.sum()==1 && m.sumOf { it[r.indexOf(1)] }==1
    }
// 0ms
    pub fn num_special(m: Vec<Vec<i32>>) -> i32 {
        m.iter().filter(|r|r.iter().sum::<i32>()==1&&
        m.iter().map(|R|R[r.iter().position(|&x|x>0).unwrap()]).sum::<i32>()==1).count() as _
    }