Ctrl K

Odd Counters After the Presses

Easy
A display board is a grid of counters with rows rows and cols columns, all starting at zero. An operator presses buttons; a press presses[i] = [r, c] adds 1 to every counter in row r and to every counter in column c (the counter where they cross gains 2). Rows and columns are numbered from zero. Return how many counters show an odd number once all the presses are done. Simulating it directly is easy; see if you can get by with O(rows + cols) memory and without walking the whole grid.
Example 1
rows=3, cols=3, presses=[[1, 2]] → 4

Row 1 and column 2 each gain 1, and the cell they share gains 2. That leaves 2 + 2 = 4 odd cells.

Example 2
rows=2, cols=4, presses=[[0, 0], [0, 3]] → 4

Row 0 is pressed twice and ends up even; columns 0 and 3 are pressed once each, so both full columns are odd: 4 cells.

Example 3
rows=1, cols=5, presses=[[0, 4]] → 4
Constraints
  • 1 ≤ rows, cols ≤ 50
  • 1 ≤ len(presses) ≤ 100
  • 0 ≤ presses[i][0] < rows
  • 0 ≤ presses[i][1] < cols
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRemainder-One Whiteboard EasyRows Rolling Back Home EasyBoost the Smallest EasyRiffle Shuffles Until the Deck Resets MediumLast One in the Circle Medium
loading Python…
Input
rows=3, cols=3, presses=[[1, 2]]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.