Skip to content

Commit 6ade429

Browse files
committed
feet: add solutions to lc problem: No.1001
No.1001.Grid Illumination
1 parent 362ffe1 commit 6ade429

File tree

4 files changed

+244
-4
lines changed

4 files changed

+244
-4
lines changed

solution/1000-1099/1001.Grid Illumination/README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,108 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
哈希表。
63+
64+
假设一盏灯的坐标为 `(x, y)`,那么它所在的行的数值为 x,列的数值为 y,正对角线的数值为 x-y,反对角线的数值为 x+y。确定某一直线的唯一数值标识后,我们就可以通过哈希表来记录某一直线所拥有的灯的数目。
65+
66+
遍历 lamps,将当前遍历到的灯所在的行、列和正、反对角线拥有灯的数目分别加 1。
67+
68+
在处理 lamps 时,需要进行去重,因为我们将重复的灯看作同一盏灯。
69+
70+
遍历 queries,判断当前查询点所在的行,列和正、反对角线是否有灯,如果有,则置 1,即该点在查询时是被照亮的。然后进行关闭操作,查找查询点所在的八近邻点及它本身是否有灯,如果有,将该点所在的行、列和正、反对角线的灯数目分别减 1,并且将灯从网格中去掉。
71+
6272
<!-- tabs:start -->
6373

6474
### **Python3**
6575

6676
<!-- 这里可写当前语言的特殊实现逻辑 -->
6777

6878
```python
69-
79+
class Solution:
80+
def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
81+
points = set()
82+
rcnt, ccnt, dgcnt, udgcnt = Counter(), Counter(), Counter(), Counter()
83+
for r, c in lamps:
84+
if (r, c) not in points:
85+
points.add((r, c))
86+
rcnt[r] += 1
87+
ccnt[c] += 1
88+
dgcnt[r - c] += 1
89+
udgcnt[r + c] += 1
90+
ans = [0] * len(queries)
91+
for i, q in enumerate(queries):
92+
r, c = q
93+
if rcnt[r] or ccnt[c] or dgcnt[r - c] or udgcnt[r + c]:
94+
ans[i] = 1
95+
for a, b in [(0, 1), (1, 0), (0, -1), (-1, 0), (0, 0), (1, 1), (-1, 1), (1, -1), (-1, -1)]:
96+
x, y = r + a, c + b
97+
if (x, y) in points:
98+
points.remove((x, y))
99+
rcnt[x] -= 1
100+
ccnt[y] -= 1
101+
dgcnt[x - y] -= 1
102+
udgcnt[x + y] -= 1
103+
return ans
70104
```
71105

72106
### **Java**
73107

74108
<!-- 这里可写当前语言的特殊实现逻辑 -->
75109

76110
```java
77-
111+
class Solution {
112+
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
113+
Set<Long> points = new HashSet<>();
114+
Map<Integer, Integer> rcnt = new HashMap<>();
115+
Map<Integer, Integer> ccnt = new HashMap<>();
116+
Map<Integer, Integer> dgcnt = new HashMap<>();
117+
Map<Integer, Integer> udgcnt = new HashMap<>();
118+
for (int[] l : lamps) {
119+
int r = l[0], c = l[1];
120+
long v = r * n + c;
121+
if (!points.contains(v)) {
122+
points.add(v);
123+
rcnt.put(r, rcnt.getOrDefault(r, 0) + 1);
124+
ccnt.put(c, ccnt.getOrDefault(c, 0) + 1);
125+
dgcnt.put(r - c, dgcnt.getOrDefault(r - c, 0) + 1);
126+
udgcnt.put(r + c, udgcnt.getOrDefault(r + c, 0) + 1);
127+
}
128+
}
129+
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {0, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
130+
int[] ans = new int[queries.length];
131+
for (int i = 0; i < queries.length; ++i) {
132+
int r = queries[i][0], c = queries[i][1];
133+
if (rcnt.getOrDefault(r, 0) > 0 || ccnt.getOrDefault(c, 0) > 0 || dgcnt.getOrDefault(r - c, 0) > 0 || udgcnt.getOrDefault(r + c, 0) > 0) {
134+
ans[i] = 1;
135+
for (int[] d : dirs) {
136+
int x = r + d[0], y = c + d[1];
137+
long v = x * n + y;
138+
if (x < 0 || x >= n || y < 0 || y >= n || !points.contains(v)) {
139+
continue;
140+
}
141+
points.remove(v);
142+
rcnt.put(x, rcnt.get(x) - 1);
143+
if (rcnt.get(x) == 0) {
144+
rcnt.remove(x);
145+
}
146+
ccnt.put(y, ccnt.get(y) - 1);
147+
if (ccnt.get(y) == 0) {
148+
ccnt.remove(y);
149+
}
150+
dgcnt.put(x - y, dgcnt.get(x - y) - 1);
151+
if (dgcnt.get(x - y) == 0) {
152+
dgcnt.remove(x - y);
153+
}
154+
udgcnt.put(x + y, udgcnt.get(x + y) - 1);
155+
if (udgcnt.get(x + y) == 0) {
156+
udgcnt.remove(x + y);
157+
}
158+
}
159+
}
160+
}
161+
return ans;
162+
}
163+
}
78164
```
79165

80166
### **...**

solution/1000-1099/1001.Grid Illumination/README_EN.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,89 @@ The 1<sup>st</sup>&nbsp;query asks if the lamp at grid[1][0] is illuminated or n
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
66+
points = set()
67+
rcnt, ccnt, dgcnt, udgcnt = Counter(), Counter(), Counter(), Counter()
68+
for r, c in lamps:
69+
if (r, c) not in points:
70+
points.add((r, c))
71+
rcnt[r] += 1
72+
ccnt[c] += 1
73+
dgcnt[r - c] += 1
74+
udgcnt[r + c] += 1
75+
ans = [0] * len(queries)
76+
for i, q in enumerate(queries):
77+
r, c = q
78+
if rcnt[r] or ccnt[c] or dgcnt[r - c] or udgcnt[r + c]:
79+
ans[i] = 1
80+
for a, b in [(0, 1), (1, 0), (0, -1), (-1, 0), (0, 0), (1, 1), (-1, 1), (1, -1), (-1, -1)]:
81+
x, y = r + a, c + b
82+
if (x, y) in points:
83+
points.remove((x, y))
84+
rcnt[x] -= 1
85+
ccnt[y] -= 1
86+
dgcnt[x - y] -= 1
87+
udgcnt[x + y] -= 1
88+
return ans
6589
```
6690

6791
### **Java**
6892

6993
```java
70-
94+
class Solution {
95+
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
96+
Set<Long> points = new HashSet<>();
97+
Map<Integer, Integer> rcnt = new HashMap<>();
98+
Map<Integer, Integer> ccnt = new HashMap<>();
99+
Map<Integer, Integer> dgcnt = new HashMap<>();
100+
Map<Integer, Integer> udgcnt = new HashMap<>();
101+
for (int[] l : lamps) {
102+
int r = l[0], c = l[1];
103+
long v = r * n + c;
104+
if (!points.contains(v)) {
105+
points.add(v);
106+
rcnt.put(r, rcnt.getOrDefault(r, 0) + 1);
107+
ccnt.put(c, ccnt.getOrDefault(c, 0) + 1);
108+
dgcnt.put(r - c, dgcnt.getOrDefault(r - c, 0) + 1);
109+
udgcnt.put(r + c, udgcnt.getOrDefault(r + c, 0) + 1);
110+
}
111+
}
112+
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {0, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
113+
int[] ans = new int[queries.length];
114+
for (int i = 0; i < queries.length; ++i) {
115+
int r = queries[i][0], c = queries[i][1];
116+
if (rcnt.getOrDefault(r, 0) > 0 || ccnt.getOrDefault(c, 0) > 0 || dgcnt.getOrDefault(r - c, 0) > 0 || udgcnt.getOrDefault(r + c, 0) > 0) {
117+
ans[i] = 1;
118+
for (int[] d : dirs) {
119+
int x = r + d[0], y = c + d[1];
120+
long v = x * n + y;
121+
if (x < 0 || x >= n || y < 0 || y >= n || !points.contains(v)) {
122+
continue;
123+
}
124+
points.remove(v);
125+
rcnt.put(x, rcnt.get(x) - 1);
126+
if (rcnt.get(x) == 0) {
127+
rcnt.remove(x);
128+
}
129+
ccnt.put(y, ccnt.get(y) - 1);
130+
if (ccnt.get(y) == 0) {
131+
ccnt.remove(y);
132+
}
133+
dgcnt.put(x - y, dgcnt.get(x - y) - 1);
134+
if (dgcnt.get(x - y) == 0) {
135+
dgcnt.remove(x - y);
136+
}
137+
udgcnt.put(x + y, udgcnt.get(x + y) - 1);
138+
if (udgcnt.get(x + y) == 0) {
139+
udgcnt.remove(x + y);
140+
}
141+
}
142+
}
143+
}
144+
return ans;
145+
}
146+
}
71147
```
72148

73149
### **...**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
3+
Set<Long> points = new HashSet<>();
4+
Map<Integer, Integer> rcnt = new HashMap<>();
5+
Map<Integer, Integer> ccnt = new HashMap<>();
6+
Map<Integer, Integer> dgcnt = new HashMap<>();
7+
Map<Integer, Integer> udgcnt = new HashMap<>();
8+
for (int[] l : lamps) {
9+
int r = l[0], c = l[1];
10+
long v = r * n + c;
11+
if (!points.contains(v)) {
12+
points.add(v);
13+
rcnt.put(r, rcnt.getOrDefault(r, 0) + 1);
14+
ccnt.put(c, ccnt.getOrDefault(c, 0) + 1);
15+
dgcnt.put(r - c, dgcnt.getOrDefault(r - c, 0) + 1);
16+
udgcnt.put(r + c, udgcnt.getOrDefault(r + c, 0) + 1);
17+
}
18+
}
19+
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {0, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
20+
int[] ans = new int[queries.length];
21+
for (int i = 0; i < queries.length; ++i) {
22+
int r = queries[i][0], c = queries[i][1];
23+
if (rcnt.getOrDefault(r, 0) > 0 || ccnt.getOrDefault(c, 0) > 0 || dgcnt.getOrDefault(r - c, 0) > 0 || udgcnt.getOrDefault(r + c, 0) > 0) {
24+
ans[i] = 1;
25+
for (int[] d : dirs) {
26+
int x = r + d[0], y = c + d[1];
27+
long v = x * n + y;
28+
if (x < 0 || x >= n || y < 0 || y >= n || !points.contains(v)) {
29+
continue;
30+
}
31+
points.remove(v);
32+
rcnt.put(x, rcnt.get(x) - 1);
33+
if (rcnt.get(x) == 0) {
34+
rcnt.remove(x);
35+
}
36+
ccnt.put(y, ccnt.get(y) - 1);
37+
if (ccnt.get(y) == 0) {
38+
ccnt.remove(y);
39+
}
40+
dgcnt.put(x - y, dgcnt.get(x - y) - 1);
41+
if (dgcnt.get(x - y) == 0) {
42+
dgcnt.remove(x - y);
43+
}
44+
udgcnt.put(x + y, udgcnt.get(x + y) - 1);
45+
if (udgcnt.get(x + y) == 0) {
46+
udgcnt.remove(x + y);
47+
}
48+
}
49+
}
50+
}
51+
return ans;
52+
}
53+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
3+
points = set()
4+
rcnt, ccnt, dgcnt, udgcnt = Counter(), Counter(), Counter(), Counter()
5+
for r, c in lamps:
6+
if (r, c) not in points:
7+
points.add((r, c))
8+
rcnt[r] += 1
9+
ccnt[c] += 1
10+
dgcnt[r - c] += 1
11+
udgcnt[r + c] += 1
12+
ans = [0] * len(queries)
13+
for i, q in enumerate(queries):
14+
r, c = q
15+
if rcnt[r] or ccnt[c] or dgcnt[r - c] or udgcnt[r + c]:
16+
ans[i] = 1
17+
for a, b in [(0, 1), (1, 0), (0, -1), (-1, 0), (0, 0), (1, 1), (-1, 1), (1, -1), (-1, -1)]:
18+
x, y = r + a, c + b
19+
if (x, y) in points:
20+
points.remove((x, y))
21+
rcnt[x] -= 1
22+
ccnt[y] -= 1
23+
dgcnt[x - y] -= 1
24+
udgcnt[x + y] -= 1
25+
return ans

0 commit comments

Comments
 (0)