当前位置:网站首页>leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
2022-07-31 05:10:00 【lin钟一】
题目链接:https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/
思路
方法一、直接模拟
直接想法
直接初始化一个m * n 的二维数组矩阵,遍历 indices 数组中的每对{ri, ci},将矩阵中的第 ri 行都加1,矩阵中的第 ci 列都加1,最后遍历矩阵记录奇数元素的个数
算法
- 初始化一个m * n的二维数组矩阵 matrix
- 遍历 indices 数组的每对{ri, ci}
1)矩阵中的第 ri 行都加1
2)矩阵中的第 ci 列都加1 - 遍历矩阵,得到奇数的数目
代码示例
func oddCells(m, n int, indices [][]int) (ans int) {
matrix := make([][]int, m)
for i := range matrix {
matrix[i] = make([]int, n)
}
for _, p := range indices {
for j := range matrix[p[0]] {
matrix[p[0]][j]++
}
for _, row := range matrix {
row[p[1]]++
}
}
for _, row := range matrix {
for _, v := range row {
ans += v & 1
}
}
return
}
复杂度分析
- 时间复杂度:O(q * (m + n) + m * n) 其中q表示 indices 数组的长度,m、n为矩阵的行数和列数,遍历 indices 数组都要更新一次行列,总共需要O(q * (m + n))的时间,最后遍历一次矩阵,总共需要O(m * n)的时间
- 空间复杂度:O(m * n) 其中m、n为矩阵的行数和列数,额外需要申请m * n的空间存放矩阵元素
方法二、模拟空间优化
直接想法
我们每次都需要遍历一整行和一整列,更新一整行和一整列的元素,极大浪费时间,我们可以使用一个row数组和col数组分别记录每一行和每一列增加的次数,我们通过计算(i, j)的位置计数即row[i] + col[j],判断当前位置是否为奇数
算法
- 初始化一个row数组和col数组
- 遍历 indices 数组中的每对{ri, ci}
1)对row[ri]的值加一
2)对col[ci]的值加一 - 遍历m * n的矩阵位置(i, j),计算(i, j)的位置计数即row[i] + col[j]判断奇数
代码示例
func oddCells(m int, n int, indices [][]int) int {
row := make([]int, m)
col := make([]int, n)
for i := range indices{
row[indices[i][0]] += 1
col[indices[i][1]] += 1
}
ans := 0
for i := 0; i < m; i++{
for j := 0; j < n; j++{
if (row[i] + col[j]) & 1 == 1{
ans++
}
}
}
return ans
}
复杂度分析
- 时间复杂度:O(q + m * n) 其中q表示 indices 数组的长度,m、n为矩阵的行数和列数遍历 indices 数组都要更新一次 总共需要O(q)的时间,最后遍历一次矩阵,总共需要O(m * n)的时间
- 空间复杂度:O(m + n) 其中 m, nm,n 为矩阵的行数与列数。需要存储矩阵的行数统计与列数统计
边栏推荐
- 剑指offer专项突击版 ---第 5 天
- [mysql improves query efficiency] Mysql database query is slow to solve the problem
- 剑指offer专项突击版 --- 第 4 天
- The process and specific code of sending SMS verification code using flask framework
- 【一起学Rust】Rust的Hello Rust详细解析
- 联盟链的真实场景在哪里
- 剑指offer专项突击版 ---- 第2天
- 【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
- Flink sink ES 写入 ES(带密码)
- 02 【el和data的两种写法 MVVM模型】
猜你喜欢
uni-app进阶之认证【day12】
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
【MQ我可以讲一个小时】
Why use Flink and how to get started with Flink?
03 【数据代理 事件处理】
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
07 【内置指令 自定义指令】
If the account number or password is entered incorrectly for many times, the account will be banned.
Refinement of the four major collection frameworks: Summary of List core knowledge
剑指offer专项突击版 ---- 第 6 天
随机推荐
变量的解构赋值
Interviewer, don't ask me to shake hands three times and wave four times again
Paginate the list collection and display the data on the page
闭包(二)
08 【生命周期 组件】
面试官问我TCP三次握手和四次挥手,我真的是
leetcode-每日一题731. 我的日程安排表 II
C语言教程(一)-准备
数据集划分以及交叉验证法
剑指offer基础版 ---- 第29天
剑指offer基础版 ---- 第26天
Redis的初识
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
Redis管道技术/分区
Why use Flink and how to get started with Flink?
剑指offer专项突击版 ---- 第 6 天
剑指offer基础版 ----- 第28天
Kubernetes 证书可用年限修改
剑指offer专项突击版 ---- 第1天
Sword Point Offer Special Assault Edition ---- Day 1