Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73a402d

Browse files
Merge pull request SharingSource#595 from SharingSource/ac_oier
✨feat: add 1252
2 parents 8701b25 + 0b4ddb8 commit 73a402d

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

‎Index/位运算.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| [693. 交替位二进制数](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/solution/gong-si-shui-by-ac_oier-zuw7/) | 简单 | 🤩🤩🤩🤩🤩 |
1919
| [762. 二进制表示中质数个计算置位](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/solution/by-ac_oier-w50x/) | 简单 | 🤩🤩🤩🤩 |
2020
| [1178. 猜字谜](https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle/solution/xiang-jin-zhu-shi-xiang-jie-po-su-wei-yu-3cr2/) | 困难 | 🤩🤩🤩🤩 |
21+
| [1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/) | [LeetCode 题解链接](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/) | 简单 | 🤩🤩🤩 |
2122
| [1711. 大餐计数](https://leetcode-cn.com/problems/count-good-meals/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-good-meals/solution/gong-shui-san-xie-xiang-jie-san-chong-gu-nn4f/) | 中等 | 🤩🤩🤩 |
2223
| [2044. 统计按位或能得到最大值的子集数目](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/solution/by-ac_oier-dos6/) | 困难 | 🤩🤩🤩🤩 |
2324
| [剑指 Offer 15. 二进制中1的个数](https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/solution/gong-shui-san-xie-yi-ti-si-jie-wei-shu-j-g9w6/) | 简单 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
| [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 |
126126
| [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 |
127127
| [1189. "气球" 的最大数量](https://leetcode-cn.com/problems/maximum-number-of-balloons/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-balloons/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9px4/) | 简单 | 🤩🤩🤩🤩 |
128+
| [1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/) | [LeetCode 题解链接](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/) | 简单 | 🤩🤩🤩 |
128129
| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 中等 | 🤩🤩🤩🤩 |
129130
| [1342. 将数字变成 0 的操作次数](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-85fb/) | 简单 | 🤩🤩🤩🤩 |
130131
| [1380. 矩阵中的幸运数](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9xwg/) | 简单 | 🤩🤩🤩 |
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「位运算」、「计数」
6+
7+
8+
9+
给你一个 $m \times n$ 的矩阵,最开始的时候,每个单元格中的值都是 0ドル$。
10+
11+
另有一个二维索引数组 `indices`,$indices[i] = [r_i, c_i]$ 指向矩阵中的某个位置,其中 $r_i$ 和 $c_i$ 分别表示指定的行和列(从 0ドル$ 开始编号)。
12+
13+
对 $indices[i]$ 所指向的每个位置,应同时执行下述增量操作:
14+
* $r_i$ 行上的所有单元格,加 1ドル$ 。
15+
* $c_i$ 列上的所有单元格,加 1ドル$ 。
16+
17+
给你 $m$、$n$ 和 $indices$ 。请你在执行完所有 $indices$ 指定的增量操作后,返回矩阵中 奇数值单元格 的数目。
18+
19+
示例 1:
20+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/06/e1.png)
21+
```
22+
输入:m = 2, n = 3, indices = [[0,1],[1,1]]
23+
24+
输出:6
25+
26+
解释:最开始的矩阵是 [[0,0,0],[0,0,0]]。
27+
第一次增量操作后得到 [[1,2,1],[0,1,0]]。
28+
最后的矩阵是 [[1,3,1],[1,3,1]],里面有 6 个奇数。
29+
```
30+
示例 2:
31+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/06/e2.png)
32+
```
33+
输入:m = 2, n = 2, indices = [[1,1],[0,0]]
34+
35+
输出:0
36+
37+
解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。
38+
```
39+
40+
提示:
41+
* 1ドル <= m, n <= 50$
42+
* 1ドル <= indices.length <= 100$
43+
* 0ドル <= r_i < m$
44+
* 0ドル <= c_i < n$
45+
46+
47+
进阶:你可以设计一个时间复杂度为 $O(n + m + indices.length)$ 且仅用 $O(n + m)$ 额外空间的算法来解决此问题吗?
48+
49+
---
50+
51+
### 基本分析
52+
53+
容易想到时间复杂度为 $O(l + m \times n),ドル空间复杂度为 $O(m + n)$ 的做法,在此不再赘述。
54+
55+
对于某个位置最终累加值为奇数的充要条件为「所在行被累加次数的奇偶性」与「所在列被累加次数的奇偶性」不同。
56+
57+
因此我们可以统计累加次数为奇数的行数 $a$(累加次数为偶数的行数为 $m - a$),累加次数为奇数的列数 $b$(累加次数为偶数的列数为 $n - b$),根据乘法原理,最终答案为 $a \times (n - b) + (m - a) \times b$。
58+
59+
---
60+
61+
### 计数模拟
62+
63+
由于我们只需关系某个位置的奇偶性,而不关心具体的累加值,我们可以创建两个数组 `r``c`,统计每行和每列的累加值的奇偶性。
64+
65+
当 $r[idx]$ 为 `True` 含义为第 $idx$ 行的累加值为奇数,否则为偶数。列数组 `c` 的统计规则同理。
66+
67+
代码:
68+
```Java
69+
class Solution {
70+
public int oddCells(int m, int n, int[][] ins) {
71+
boolean[] r = new boolean[m], c = new boolean[n];
72+
int a = 0, b = 0;
73+
for (int[] info : ins) {
74+
a += (r[info[0]] = !r[info[0]]) ? 1 : -1;
75+
b += (c[info[1]] = !c[info[1]]) ? 1 : -1;
76+
}
77+
return a * (n - b) + (m - a) * b;
78+
}
79+
}
80+
```
81+
* 时间复杂度:构建计数数组的复杂度为 $O(m + n),ドル统计奇数行和奇数列复杂度为 $O(l),ドル其中 $l$ 为数组 `ins` 的长度,复杂度为 $O(m + n + l)$
82+
* 空间复杂度:$O(m + n)$
83+
84+
---
85+
86+
### 位运算
87+
88+
更进一步,我们可以使用两个 `long` 变量 $c1$ 和 $c2$ 来分别充当行和列的计数数组,当 $c1$ 的第 $k$ 位为 1ドル,ドル代表第 $k$ 行累加值为奇数,当 $c1$ 的第 $k$ 位为 0ドル,ドル代表第 $k$ 行累加值为偶数;$c2$ 的计数规则同理。而翻转二进制中的某一位可使用「异或」操作。
89+
90+
当处理完所有的 `ins` 之后,可通过「遍历 $c1$ 的低 $m$ 位 + 遍历 $c2$ 的低 $n$ 位」来得到行数中奇数个数 $a,ドル列数中奇数个数 $b,ドル复杂度为 $O(m + n)$;也使用 `bitCount` 统计 `long` 二进制数中 1ドル$ 的个数(本质是分治操作),复杂度为 $O(\log{64})$。
91+
92+
代码:
93+
```Java
94+
class Solution {
95+
public int oddCells(int m, int n, int[][] ins) {
96+
long c1 = 0, c2 = 0;
97+
for (int[] info : ins) {
98+
c1 ^= 1L << info[0];
99+
c2 ^= 1L << info[1];
100+
}
101+
int a = 0, b = 0;
102+
for (int i = 0; i < m; i++) a += ((c1 >> i) & 1);
103+
for (int i = 0; i < n; i++) b += ((c2 >> i) & 1);
104+
return a * (n - b) + (m - a) * b;
105+
}
106+
}
107+
```
108+
109+
-
110+
111+
```Java
112+
class Solution {
113+
public int oddCells(int m, int n, int[][] ins) {
114+
long c1 = 0, c2 = 0;
115+
for (int[] info : ins) {
116+
c1 ^= 1L << info[0];
117+
c2 ^= 1L << info[1];
118+
}
119+
int a = Long.bitCount(c1), b = Long.bitCount(c2);
120+
return a * (n - b) + (m - a) * b;
121+
}
122+
}
123+
```
124+
* 时间复杂度:处理所有的 `ins` 复杂度为 $O(l),ドル其中 $l$ 为数组 `ins` 的长度;使用遍历方式统计奇数行和奇数列个数复杂度为 $O(m + n)$;使用 `bitCount` 操作统计二进制中 1ドル$ 个数,复杂度为 $O(\log{C}),ドル其中 $C = 64$ 为 `long` 二进制数长度,整体复杂度为 $O(l + m + n)$ 或 $O(l + \log{C})$
125+
* 空间复杂度:$O(1)$
126+
127+
---
128+
129+
### 最后
130+
131+
这是我们「刷穿 LeetCode」系列文章的第 `No.1252` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
132+
133+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
134+
135+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
136+
137+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
138+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /