Problem 232921 · medium · Phase 02 Linear Data Structures

Balanced Cells

2d-lists · sums · precomputation

A cell of a grid is balanced when the sum of its row equals the sum of its column. Given a grid of integers grid, return how many cells are balanced.

Examples

Input:  grid = [[1, 2, 0],
                [0, 1, 0]]
Output: 2
Explanation: row sums are 3 and 1; column sums are 1, 3 and 0.
Cell (0,1) has row sum 3 and column sum 3; cell (1,0) has row sum 1 and column sum 1.

Input:  grid = [[2, 1],
                [1, 2]]
Output: 4

Input:  grid = [[5]]
Output: 1

Constraints

  • 1 <= rows, cols <= 100, -1000 <= grid[r][c] <= 1000

Goals

  • Precompute row and column sums before the main loop
  • Count cells satisfying a condition on their row and column
Starting Python…