当前位置:网站首页>【LeetCode】1252. Number of odd value cells
【LeetCode】1252. Number of odd value cells
2022-07-16 07:31:00 【pass night】
subject
To give you one m x n Matrix , At the very beginning , The value in each cell is 0.
There is another two-dimensional index array indices,indices[i] = [ri, ci] Point to a position in the matrix , among ri and ci Respectively represent the specified row and column ( from 0 Numbered starting ).
Yes indices[i] Every position pointed , The following incremental operations should be performed at the same time :
riAll cells on the row , Add1.ciAll cells on the column , Add1.
Here you are. m、n and indices . Please finish executing all indices After the specified incremental operation , Back in the matrix Odd cells Number of .
Example 1:

Input :m = 2, n = 3, indices = [[0,1],[1,1]]
Output :6
explain : The initial matrix is [[0,0,0],[0,0,0]].
After the first incremental operation [[1,2,1],[0,1,0]].
The final matrix is [[1,3,1],[1,3,1]], There are 6 An odd number .
Example 2:

Input :m = 2, n = 2, indices = [[1,1],[0,0]]
Output :0
explain : The final matrix is [[2,2],[2,2]], There are no odd numbers in it .
Tips :
1 <= m, n <= 501 <= indices.length <= 1000 <= ri < m0 <= ci < n
** Advanced :** You can design a time complexity of O(n + m + indices.length) And only O(n + m) Extra space algorithm to solve this problem ?
Ideas
- Because each operation is for an entire row or column , So declare two variables to save the changes of the row and column
- matrix[i][j] The change of is equivalent to the third i Xing He j Sum of changes in columns
Code
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
row,col = [0] * m, [0] * n
for r,c in indices:
row[r] += 1
col[c] += 1
ret = 0
for i in range(m):
for j in range(n):
if (row[i] + col[j])%2==1: ret +=1
return ret
Complexity
- Time complexity : O ( m + n + i n d i c e s . l e n g t h ) O(m+n+indices.length) O(m+n+indices.length)
- Spatial complexity : O ( m + n ) O(m+n) O(m+n)
边栏推荐
- Leetcode lecture - 1217 Play chips (difficulty: simple)
- Type erase & bridge method
- What is EventLoop?
- New features of es6-es11 (this article is enough)
- "The following signature cannot be verified because there is no public key" solution
- C#-Mathf
- Men should be "strong", not "soft", "weak" and "empty"
- Personal blog learning records
- 《MySQL数据库原理、设计与应用》课后习题及答案 黑马程序员编著
- 【6月5号学习记录】
猜你喜欢

Implementation of hash table linear detection class template

Get started with promise

7、 Experimental report of security implementation scheme in San and NAS environment

【Oracle】在docker中配置Oracle数据库

SAP BW extraction layer error s:aa 821 (bukrs)

闭包那点事儿

Go seckill system 3 -- project structure construction and commodity model development.

一、磁盘数据恢复实验报告

368. Maximum divisible subset dynamic programming

SAP DUMP CALLBACK_ REJECTED_ BY_ WHITELIST - SE51, RSSCREENPAINTER
随机推荐
从功能测试到自动化测试,实现薪资翻倍,我整理的超全学习指南【附学习笔记】
【LeetCode】2028. Find the missing observation data
闭包那点事儿
SAP OPEN SQL
六、数据备份软件的配置实验报告
MVCC多版本并发控制
Recursion in binary tree
求职3个月,简历大多都石沉大海,一说是手工测试都连连摇头~太难了
Hash table
一、磁盘数据恢复实验报告
【LeetCode】442. 数组中重复的数据
Implementation method of three column layout (generally, it is required to write as much as possible)
Five principles of aiops landing (3): Architecture route
Implementation of hash table linear detection class template
Determining whether there are rings in a directed graph by topological sorting
SAP T-CODE 事务码集(持续更新)
SAP Tables 透明表、视图(持续更新)
ScheduledThreadPoolExecutor源码和误区详解
【LeetCode】2024. The most perplexing degree of examination
Rocket directory