给你一个 n 行 m 列的矩阵,最开始的时候,每个单元格中的值都是 0。
另有一个索引数组 indices,indices[i] = [ri, ci] 中的 ri 和 ci 分别表示指定的行和列(从 0 开始编号)。
你需要将每对 [ri, ci] 指定的行和列上的所有单元格的值加 1。
请你在执行完所有 indices 指定的增量操作后,返回矩阵中 「奇数值单元格」 的数目。
示例 1:
输入:n = 2, m = 3, indices = [[0,1],[1,1]]
输出:6
解释:最开始的矩阵是 [[0,0,0],[0,0,0]]。
第一次增量操作后得到 [[1,2,1],[0,1,0]]。
最后的矩阵是 [[1,3,1],[1,3,1]],里面有 6 个奇数。
示例 2:
输入:n = 2, m = 2, indices = [[1,1],[0,0]]
输出:0
解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。
提示:
1 <= n <= 50
1 <= m <= 50
1 <= indices.length <= 100
0 <= indices[i][0] < n
0 <= indices[i][1] < m
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cells-with-odd-values-in-a-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1
暴力法
class Solution(object):
def oddCells(self, n, m, indices):
"""
:type n: int
:type m: int
:type indices: List[List[int]]
:rtype: int
"""
res = [[0] * m for i in range(n)]
for x, y in indices:
for i in range(m):
res[x][i] += 1
for j in range(n):
res[j][y] += 1
return sum(j % 2 == 1 for i in res for j in i)
执行结果:
通过
显示详情
执行用时:28 ms, 在所有 Python 提交中击败了89.93%的用户
内存消耗:12.7 MB, 在所有 Python 提交中击败了33.33%的用户
思路2
分别计算行与列,x行y列对应的值为rows[x]+cols[y],最终统计奇数的个数
class Solution(object):
def oddCells(self, n, m, indices):
"""
:type n: int
:type m: int
:type indices: List[List[int]]
:rtype: int
"""
rows=[0]*n
cols=[0]*m
for x,y in indices:
rows[x]+=1
cols[y]+=1
return sum((rows[i]+cols[j])%2==1 for i in range(n) for j in range(m))
执行结果:
通过
显示详情
执行用时:20 ms, 在所有 Python 提交中击败了98.56%的用户
内存消耗:12.8 MB, 在所有 Python 提交中击败了33.33%的用户