From 0b4ddb89c364160c45aed2266badc859235758ce Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 12 Jul 2022 10:16:45 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=201252?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\215\350\277\220\347\256\227.md" | 1 + "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 138 ++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 "LeetCode/1251-1260/1252. \345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\344\275\215\350\277\220\347\256\227.md" "b/Index/\344\275\215\350\277\220\347\256\227.md" index 5ae96811..f6e0516f 100644 --- "a/Index/\344\275\215\350\277\220\347\256\227.md" +++ "b/Index/\344\275\215\350\277\220\347\256\227.md" @@ -18,6 +18,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | | [剑指 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/) | 简单 | 🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index cdcb6142..03321d85 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -125,6 +125,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | diff --git "a/LeetCode/1251-1260/1252. \345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1251-1260/1252. \345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..248f94da --- /dev/null +++ "b/LeetCode/1251-1260/1252. \345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,138 @@ +### 题目描述 + +这是 LeetCode 上的 **[1252. 奇数值单元格的数目](https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/solution/by-ac_oier-p0za/)** ,难度为 **简单**。 + +Tag : 「模拟」、「位运算」、「计数」 + + + +给你一个 $m \times n$ 的矩阵,最开始的时候,每个单元格中的值都是 $0$。 + +另有一个二维索引数组 `indices`,$indices[i] = [r_i, c_i]$ 指向矩阵中的某个位置,其中 $r_i$ 和 $c_i$ 分别表示指定的行和列(从 $0$ 开始编号)。 + +对 $indices[i]$ 所指向的每个位置,应同时执行下述增量操作: +* $r_i$ 行上的所有单元格,加 $1$ 。 +* $c_i$ 列上的所有单元格,加 $1$ 。 + +给你 $m$、$n$ 和 $indices$ 。请你在执行完所有 $indices$ 指定的增量操作后,返回矩阵中 奇数值单元格 的数目。 + +示例 1: +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/06/e1.png) +``` +输入:m = 2, n = 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: +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/06/e2.png) +``` +输入:m = 2, n = 2, indices = [[1,1],[0,0]] + +输出:0 + +解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。 +``` + +提示: +* $1 <= m, n <= 50$ +* $1 <= indices.length <= 100$ +* $0 <= r_i < m$ +* $0 <= c_i < n$ + + +进阶:你可以设计一个时间复杂度为 $O(n + m + indices.length)$ 且仅用 $O(n + m)$ 额外空间的算法来解决此问题吗? + +--- + +### 基本分析 + +容易想到时间复杂度为 $O(l + m \times n)$,空间复杂度为 $O(m + n)$ 的做法,在此不再赘述。 + +对于某个位置最终累加值为奇数的充要条件为「所在行被累加次数的奇偶性」与「所在列被累加次数的奇偶性」不同。 + +因此我们可以统计累加次数为奇数的行数 $a$(累加次数为偶数的行数为 $m - a$),累加次数为奇数的列数 $b$(累加次数为偶数的列数为 $n - b$),根据乘法原理,最终答案为 $a \times (n - b) + (m - a) \times b$。 + +--- + +### 计数模拟 + +由于我们只需关系某个位置的奇偶性,而不关心具体的累加值,我们可以创建两个数组 `r` 和 `c`,统计每行和每列的累加值的奇偶性。 + +当 $r[idx]$ 为 `True` 含义为第 $idx$ 行的累加值为奇数,否则为偶数。列数组 `c` 的统计规则同理。 + +代码: +```Java +class Solution { + public int oddCells(int m, int n, int[][] ins) { + boolean[] r = new boolean[m], c = new boolean[n]; + int a = 0, b = 0; + for (int[] info : ins) { + a += (r[info[0]] = !r[info[0]]) ? 1 : -1; + b += (c[info[1]] = !c[info[1]]) ? 1 : -1; + } + return a * (n - b) + (m - a) * b; + } +} +``` +* 时间复杂度:构建计数数组的复杂度为 $O(m + n)$,统计奇数行和奇数列复杂度为 $O(l)$,其中 $l$ 为数组 `ins` 的长度,复杂度为 $O(m + n + l)$ +* 空间复杂度:$O(m + n)$ + +--- + +### 位运算 + +更进一步,我们可以使用两个 `long` 变量 $c1$ 和 $c2$ 来分别充当行和列的计数数组,当 $c1$ 的第 $k$ 位为 $1$,代表第 $k$ 行累加值为奇数,当 $c1$ 的第 $k$ 位为 $0$,代表第 $k$ 行累加值为偶数;$c2$ 的计数规则同理。而翻转二进制中的某一位可使用「异或」操作。 + +当处理完所有的 `ins` 之后,可通过「遍历 $c1$ 的低 $m$ 位 + 遍历 $c2$ 的低 $n$ 位」来得到行数中奇数个数 $a$,列数中奇数个数 $b$,复杂度为 $O(m + n)$;也使用 `bitCount` 统计 `long` 二进制数中 $1$ 的个数(本质是分治操作),复杂度为 $O(\log{64})$。 + +代码: +```Java +class Solution { + public int oddCells(int m, int n, int[][] ins) { + long c1 = 0, c2 = 0; + for (int[] info : ins) { + c1 ^= 1L << info[0]; + c2 ^= 1L << info[1]; + } + int a = 0, b = 0; + for (int i = 0; i < m; i++) a += ((c1 >> i) & 1); + for (int i = 0; i < n; i++) b += ((c2 >> i) & 1); + return a * (n - b) + (m - a) * b; + } +} +``` + +- + +```Java +class Solution { + public int oddCells(int m, int n, int[][] ins) { + long c1 = 0, c2 = 0; + for (int[] info : ins) { + c1 ^= 1L << info[0]; + c2 ^= 1L << info[1]; + } + int a = Long.bitCount(c1), b = Long.bitCount(c2); + return a * (n - b) + (m - a) * b; + } +} +``` +* 时间复杂度:处理所有的 `ins` 复杂度为 $O(l)$,其中 $l$ 为数组 `ins` 的长度;使用遍历方式统计奇数行和奇数列个数复杂度为 $O(m + n)$;使用 `bitCount` 操作统计二进制中 $1$ 个数,复杂度为 $O(\log{C})$,其中 $C = 64$ 为 `long` 二进制数长度,整体复杂度为 $O(l + m + n)$ 或 $O(l + \log{C})$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1252` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +