LeetCode Entry
2482. Difference Between Ones and Zeros in Row and Column
diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
2482. Difference Between Ones and Zeros in Row and Column easy
blog post
substack

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/438
Problem TLDR
diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
Complexity
-
Time complexity: \(O(nm)\)
-
Space complexity: \(O(nm)\)
Code
fun onesMinusZeros(grid: Array<IntArray>): Array<IntArray> {
val onesRow = grid.map { it.count { it == 1 } }
val zerosRow = grid.map { it.count { it == 0 } }
val onesCol = grid[0].indices.map { x -> grid.indices.count { grid[it][x] == 1 } }
val zerosCol = grid[0].indices.map { x -> grid.indices.count { grid[it][x] == 0 } }
return Array(grid.size) { y -> IntArray(grid[0].size) { x ->
onesRow[y] + onesCol[x] - zerosRow[y] - zerosCol[x]
}}
}