1252. Cells with Odd Values in a Matrix

Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.

Return the number of cells with odd values in the matrix after applying the increment to all indices.

分别统计行和列的次数,然后统计次数为奇数的行数r和列数c,那么答案是r*m + c*n - 2(r*c)

class Solution(object):
    def oddCells(self, n, m, indices):
        """
        :type n: int
        :type m: int
        :type indices: List[List[int]]
        :rtype: int
        """
        row = [0] * n
        col = [0] * m
        for x in indices:
            row[x[0]] += 1
            col[x[1]] += 1
        r = 0
        c = 0
        for x in row:
            if x % 2 == 1:
                r += 1
        for x in col:
            if x % 2 == 1:
                c += 1
        print(r, c)
        return r * m + c * n - 2 * (r * c)

 

posted @ 2020-06-29 10:51  whatyouthink  阅读(85)  评论(0编辑  收藏  举报