Skip to content

Commit bfcf7be

Browse files
committed
✨feat: Add 1001
1 parent b9a4252 commit bfcf7be

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

Index/哈希表.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
4040
| [981. 基于时间的键值存储](https://leetcode-cn.com/problems/time-based-key-value-store/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/time-based-key-value-store/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-h5et/) | 中等 | 🤩🤩🤩🤩 |
4141
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩 |
42+
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
4243
| [1074. 元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-uttw/) | 困难 | 🤩🤩🤩 |
4344
| [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/) | 困难 | 🤩🤩🤩🤩 |
4445
| [1218. 最长定差子序列](https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/solution/gong-shui-san-xie-jie-he-tan-xin-de-zhua-dj1k/) | 中等 | 🤩🤩🤩🤩🤩 |

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 |
7777
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
7878
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
79+
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
7980
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
8081
| [1047. 删除字符串中的所有相邻重复项](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/solution/cong-30-dao-100wu-chong-shi-xian-jie-jue-vkah/) | 简单 | 🤩🤩🤩🤩 |
8182
| [1078. Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/occurrences-after-bigram/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-qyki/) | 简单 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/)** ,难度为 **困难**
4+
5+
Tag : 「模拟」、「哈希表」
6+
7+
8+
9+
在大小为 `n x n` 的网格 `grid` 上,每个单元格都有一盏灯,最初灯都处于 关闭 状态。
10+
11+
给你一个由灯的位置组成的二维数组 `lamps`,其中 $lamps[i] = [row_i, col_i]$ 表示 打开 位于 `grid[rowi][coli]` 的灯。即便同一盏灯可能在 `lamps` 中多次列出,不会影响这盏灯处于 打开 状态。
12+
13+
当一盏灯处于打开状态,它将会照亮 自身所在单元格 以及同一 行 、同一 列 和两条 对角线 上的 所有其他单元格 。
14+
15+
另给你一个二维数组 `queries`,其中 $queries[j] = [row_j, col_j]$ 。对于第 $j$ 个查询,如果单元格 $[row_j, col_j]$ 是被照亮的,则查询结果为 $1$ ,否则为 $0$ 。
16+
17+
在第 `j` 次查询之后 [按照查询的顺序] ,关闭 位于单元格 $grid[row_j][col_j]$ 上及相邻 $8$ 个方向上(与单元格 $grid[row_i][col_i]$ 共享角或边)的任何灯。
18+
19+
返回一个整数数组 `ans` 作为答案, `ans[j]` 应等于第 `j` 次查询 `queries[j]` 的结果,$1$ 表示照亮,$0$ 表示未照亮。
20+
21+
示例 1:
22+
![](https://assets.leetcode.com/uploads/2020/08/19/illu_1.jpg)
23+
```
24+
输入:n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
25+
26+
输出:[1,0]
27+
```
28+
![](https://assets.leetcode.com/uploads/2020/08/19/illu_step1.jpg)
29+
```
30+
解释:最初所有灯都是关闭的。在执行查询之前,打开位于 [0, 0] 和 [4, 4] 的灯。第 0 次查询检查 grid[1][1] 是否被照亮(蓝色方框)。该单元格被照亮,所以 ans[0] = 1 。然后,关闭红色方框中的所有灯。
31+
```
32+
![](https://assets.leetcode.com/uploads/2020/08/19/illu_step2.jpg)
33+
```
34+
第 1 次查询检查 grid[1][0] 是否被照亮(蓝色方框)。该单元格没有被照亮,所以 ans[1] = 0 。然后,关闭红色矩形中的所有灯。
35+
```
36+
示例 2:
37+
```
38+
输入:n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]
39+
40+
输出:[1,1]
41+
```
42+
示例 3:
43+
```
44+
输入:n = 5, lamps = [[0,0],[0,4]], queries = [[0,4],[0,1],[1,4]]
45+
46+
输出:[1,1,0]
47+
```
48+
49+
提示:
50+
* $1 <= n <= 10^9$
51+
* $0 <= lamps.length <= 20000$
52+
* $0 <= queries.length <= 20000$
53+
* $lamps[i].length == 2$
54+
* $0 <= row_i, col_i < n$
55+
* $queries[j].length == 2$
56+
* $0 <= row_j, col_j < n$
57+
58+
---
59+
60+
### 哈希表 + 线映射
61+
62+
棋盘大小的数据范围为 $n = 10^9$,硬模拟「亮灯」的操作必然会 `TLE`,而 `lamps``queries` 数据范围为 $20000$ 是一个较为常见的突破口。
63+
64+
由于点亮每一盏灯,可以使得当前 **行、列 和 对角线** 的位置被照亮,行列可直接使用棋盘坐标的 $(x, y)$ 来代指,而对角线则可以使用「截距」来进行代指,即使用 $x + y$ 和 $x - y$ 进行代指。
65+
66+
分别使用四个「哈希表」`row``col``left``right` 来记录 **行、列 和 对角线** 的点亮情况(`key` 为线编号,`value` 为点亮次数)。
67+
68+
这样我们可以在 $O(1)$ 的复杂度处理掉所有的 $lamps[i]$,某个位置被照亮的充要条件为:**「当前位置所在行被点亮」或「当前位置所在列被点亮」或「当前位置所处的对角线被点亮」。**
69+
70+
同时,由于每次查询后要进行「灭灯」操作(注意:灭灯只能灭有灯的位置,而不是灭有光的位置 🤣),因此我们还需要另外记录每个灯的位置,可以使用利用「二维转一维」的技巧进行编号:$idx = x * n + y$,并使用 `HashSet` 进行记录(忽略重复的 $lamps[i]$)。
71+
72+
由于询问次数最多为 $20000$,因此直接在查询完成后模拟「灭灯」即可(访问自身及相邻格子,共 $9$ 个),计算量为 $2 * 10^5$ 以内,可以接受。若某个位置存在灯,将其从 `HashSet` 中移除,并更新对应线的点亮情况。
73+
74+
代码:
75+
```Java
76+
class Solution {
77+
int[][] dirs = new int[][]{{0,0},{0,-1},{0,1},{-1,0},{-1,-1},{-1,1},{1,0},{1,-1},{1,1}};
78+
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
79+
long N = n;
80+
Map<Integer, Integer> row = new HashMap<>(), col = new HashMap<>();
81+
Map<Integer, Integer> left = new HashMap<>(), right = new HashMap<>();
82+
Set<Long> set = new HashSet<>();
83+
for (int[] l : lamps) {
84+
int x = l[0], y = l[1];
85+
int a = x + y, b = x - y;
86+
if (set.contains(x * N + y)) continue;
87+
increment(row, x); increment(col, y);
88+
increment(left, a); increment(right, b);
89+
set.add(x * N + y);
90+
}
91+
int m = queries.length;
92+
int[] ans = new int[m];
93+
for (int i = 0; i < m; i++) {
94+
int[] q = queries[i];
95+
int x = q[0], y = q[1];
96+
int a = x + y, b = x - y;
97+
if (row.containsKey(x) || col.containsKey(y) || left.containsKey(a) || right.containsKey(b)) ans[i] = 1;
98+
99+
for (int[] d : dirs) {
100+
int nx = x + d[0], ny = y + d[1];
101+
int na = nx + ny, nb = nx - ny;
102+
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
103+
if (set.contains(nx * N + ny)) {
104+
set.remove(nx * N + ny);
105+
decrement(row, nx); decrement(col, ny);
106+
decrement(left, na); decrement(right, nb);
107+
}
108+
}
109+
}
110+
return ans;
111+
}
112+
void increment(Map<Integer, Integer> map, int key) {
113+
map.put(key, map.getOrDefault(key, 0) + 1);
114+
}
115+
void decrement(Map<Integer, Integer> map, int key) {
116+
if (map.get(key) == 1) map.remove(key);
117+
else map.put(key, map.get(key) - 1);
118+
}
119+
}
120+
```
121+
* 时间复杂度:令 $lamps$ 长度为 $a$,$queries$ 长度为 $b$,处理所有的 $lamp[i]$ 复杂度为 $O(a)$,处理所有的 $queries[j]$ 复杂度为 $O(C * b)$,其中 $C$ 为模拟灭灯时所联通的格子数量,固定为 $9$。整体复杂度为 $O(a + C * b)$
122+
* 空间复杂度:$O(a)$
123+
124+
---
125+
126+
### 最后
127+
128+
这是我们「刷穿 LeetCode」系列文章的第 `No.1001` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
129+
130+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
131+
132+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
133+
134+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
135+

0 commit comments

Comments
 (0)