Cells with Odd Values in a Matrix Lab
In this lab, you are tasked to implement a function that calculates the number of odd-valued cells in a matrix after applying increment operations. The matrix is an
m x n matrix, initialized to all
0's. There is also a 2D array
indices where each
indices[i] = [ri, ci] represents a 0-indexed location to perform some increment operations on the matrix.
For each location
indices[i], do both of the following:
- Increment all the cells on row
- Increment all the cells on column
The function should be named
oddCells and should have the following signature:
oddCells(m: number, n: number, indices: number): number
Input: m = 2, n = 3, indices = [[0,1],[1,1]] Output: 6
Initial matrix =
After applying first increment it becomes
The final matrix is
[[1,3,1],[1,3,1]], which contains 6 odd numbers.
Input: m = 2, n = 2, indices = [[1,1],[0,0]] Output: 0
Final matrix =
[[2,2],[2,2]]. There are no odd numbers in the final matrix.
1 <= m, n <= 50
1 <= indices.length <= 100
0 <= ri < m
0 <= ci < n
- Implement the
- Export the
oddCellsfunction using ESM import/export syntax.
- Optimize your solution to have a time complexity of
O(n + m + indices.length)with only
O(n + m)extra space.