Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1252 - Cells with Odd Values in a Matrix.

    Easy

    There is an m x n matrix that is initialized to all 0's. There is also a 2D array indices where each <code>indicesi = r<sub>i</sub>, c<sub>i</sub></code> 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 <code>r<sub>i</sub></code>.

    • Increment all the cells on column <code>c<sub>i</sub></code>.

    Given m, n, and indices, return the number of odd-valued cells in the matrix after applying the increment to all locations in indices.

    Example 1:

    Input: m = 2, n = 3, indices = [0,1,1,1]

    Output: 6

    Explanation: Initial matrix = [0,0,0,0,0,0].

    After applying first increment it becomes [1,2,1,0,1,0].

    The final matrix is [1,3,1,1,3,1], which contains 6 odd numbers.

    Example 2:

    Input: m = 2, n = 2, indices = [1,1,0,0]

    Output: 0

    Explanation: Final matrix = [2,2,2,2]. There are no odd numbers in the final matrix.

    Constraints:

    • 1 &lt;= m, n &lt;= 50

    • 1 &lt;= indices.length &lt;= 100

    • <code>0 <= r<sub>i</sub>< m</code>

    • <code>0 <= c<sub>i</sub>< n</code>

    Follow up: Could you solve this in O(n + m + indices.length) time with only O(n + m) extra space?

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer oddCells(Integer n, Integer m, Array<IntArray> indices)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait