Skip to content

Commit d078ddb

Browse files
committed
feat: leetcode 1252
1 parent b7fe42c commit d078ddb

File tree

8 files changed

+138
-1
lines changed

8 files changed

+138
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:428-green" alt="进度:428">
5+
<img src="https://img.shields.io/badge/-进度:429-green" alt="进度:429">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,12 @@
21172117
"path": "./problemset/defanging-an-ip-address/README.md",
21182118
"difficulty": "简单"
21192119
},
2120+
{
2121+
"id": "1252",
2122+
"title": "奇数值单元格的数目",
2123+
"path": "./problemset/cells-with-odd-values-in-a-matrix/README.md",
2124+
"difficulty": "简单"
2125+
},
21202126
{
21212127
"id": "1342",
21222128
"title": "将数字变成 0 的操作次数",

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,16 @@
38793879
"url": "https://leetcode.cn/problems/minimum-cost-to-move-chips-to-the-same-position/",
38803880
"path": "./problemset/minimum-cost-to-move-chips-to-the-same-position/README.md"
38813881
},
3882+
{
3883+
"id": "1252",
3884+
"title": {
3885+
"cn": "奇数值单元格的数目",
3886+
"en": "cells-with-odd-values-in-a-matrix"
3887+
},
3888+
"difficulty": "简单",
3889+
"url": "https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/",
3890+
"path": "./problemset/cells-with-odd-values-in-a-matrix/README.md"
3891+
},
38823892
{
38833893
"id": "1305",
38843894
"title": {

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@
394394
| 969. [煎饼排序](../../problemset/pancake-sorting/README.md) | 中等 |
395395
| 1089. [复写零](../../problemset/duplicate-zeros/README.md) | 简单 |
396396
| 1108. [IP地址无效化](../../problemset/defanging-an-ip-address/README.md) | 简单 |
397+
| 1252. [奇数值单元格的数目](../../problemset/cells-with-odd-values-in-a-matrix/README.md) | 简单 |
397398
| 1342. [将数字变成 0 的操作次数](../../problemset/number-of-steps-to-reduce-a-number-to-zero/README.md) | 简单 |
398399
| 1606. [找到处理最多请求的服务器](../../problemset/find-servers-that-handled-most-number-of-requests/README.md) | 困难 |
399400
| 1672. [最富有客户的资产总量](../../problemset/richest-customer-wealth/README.md) | 简单 |

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,8 @@
776776

777777
[1217. 玩筹码](../../problemset/minimum-cost-to-move-chips-to-the-same-position/README.md)
778778

779+
[1252. 奇数值单元格的数目](../../problemset/cells-with-odd-values-in-a-matrix/README.md)
780+
779781
[1305. 两颗二叉搜索树中的所有元素](../../problemset/all-elements-in-two-binary-search-trees/README.md)
780782

781783
[1342. 将数字变成 0 的操作次数](../../problemset/number-of-steps-to-reduce-a-number-to-zero/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 奇数值单元格的数目
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/
6+
7+
## 题目
8+
9+
给你一个 `m x n` 的矩阵,最开始的时候,每个单元格中的值都是 `0`
10+
11+
另有一个二维索引数组 `indices``indices[i] = [ri, ci]` 指向矩阵中的某个位置,其中 `ri``ci` 分别表示指定的行和列(从 `0` 开始编号)。
12+
13+
`indices[i]` 所指向的每个位置,应同时执行下述增量操作:
14+
15+
- `ri` 行上的所有单元格,加 `1`
16+
- `ci` 列上的所有单元格,加 `1`
17+
18+
给你 `m``n``indices` 。请你在执行完所有 `indices` 指定的增量操作后,返回矩阵中 **奇数值单元格** 的数目。
19+
20+
### 示例
21+
22+
#### 示例 1:
23+
24+
![image](https://user-images.githubusercontent.com/54696834/178393373-68b458b2-f46a-4208-bc52-b96555eadb98.png)
25+
26+
```
27+
输入:m = 2, n = 3, indices = [[0,1],[1,1]]
28+
输出:6
29+
解释:最开始的矩阵是 [[0,0,0],[0,0,0]]。
30+
第一次增量操作后得到 [[1,2,1],[0,1,0]]。
31+
最后的矩阵是 [[1,3,1],[1,3,1]],里面有 6 个奇数。
32+
```
33+
34+
#### 示例 2:
35+
36+
![image](https://user-images.githubusercontent.com/54696834/178393381-68c8d779-68cf-4164-a043-bcd858a6bcba.png)
37+
38+
```
39+
输入:m = 2, n = 2, indices = [[1,1],[0,0]]
40+
输出:0
41+
解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。
42+
```
43+
44+
## 解题
45+
46+
```ts
47+
/**
48+
* 模拟
49+
* @desc 时间复杂度 O(q + m + n) 空间复杂度 O(m + n)
50+
* @param m
51+
* @param n
52+
* @param indices
53+
* @returns
54+
*/
55+
export function oddCells(m: number, n: number, indices: number[][]): number {
56+
const rows = new Array(m).fill(0)
57+
const cols = new Array(n).fill(0)
58+
59+
for (const [r, c] of indices) {
60+
rows[r]++
61+
cols[c]++
62+
}
63+
64+
let oddx = 0
65+
let oddy = 0
66+
67+
for (const row of rows)
68+
if ((row & 1) !== 0) oddx++
69+
70+
for (const col of cols)
71+
if ((col & 1) !== 0) oddy++
72+
73+
return oddx * (n - oddy) + (m - oddx) * oddy
74+
}
75+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { oddCells } from '.'
3+
4+
describe('奇数值单元格的数目', () => {
5+
testCase(oddCells)
6+
})
7+
8+
function testCase(fn: (m: number, n: number, indices: number[][]) => number) {
9+
it.each([
10+
[2, 3, [[0, 1], [1, 1]], 6],
11+
[2, 2, [[1, 1], [0, 0]], 0],
12+
])('示例%#', (m, n, indices, expected) => {
13+
expect(fn(m, n, indices)).toBe(expected)
14+
})
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 模拟
3+
* @desc 时间复杂度 O(q + m + n) 空间复杂度 O(m + n)
4+
* @param m
5+
* @param n
6+
* @param indices
7+
* @returns
8+
*/
9+
export function oddCells(m: number, n: number, indices: number[][]): number {
10+
const rows = new Array(m).fill(0)
11+
const cols = new Array(n).fill(0)
12+
13+
for (const [r, c] of indices) {
14+
rows[r]++
15+
cols[c]++
16+
}
17+
18+
let oddx = 0
19+
let oddy = 0
20+
21+
for (const row of rows)
22+
if ((row & 1) !== 0) oddx++
23+
24+
for (const col of cols)
25+
if ((col & 1) !== 0) oddy++
26+
27+
return oddx * (n - oddy) + (m - oddx) * oddy
28+
}

0 commit comments

Comments
 (0)