-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridIllumination.java
95 lines (87 loc) · 4 KB
/
GridIllumination.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.hncboy;
import java.util.*;
/**
* @author hncboy
* @date 2022/2/8 20:39
* 1001.网格照明
*
* 在大小为 n x n 的网格 grid 上,每个单元格都有一盏灯,最初灯都处于 关闭 状态。
*
* 给你一个由灯的位置组成的二维数组 lamps ,其中 lamps[i] = [rowi, coli] 表示 打开 位于 grid[rowi][coli] 的灯。
* 即便同一盏灯可能在 lamps 中多次列出,不会影响这盏灯处于 打开 状态。
*
* 当一盏灯处于打开状态,它将会照亮 自身所在单元格 以及同一 行 、同一 列 和两条 对角线 上的 所有其他单元格 。
*
* 另给你一个二维数组 queries ,其中 queries[j] = [rowj, colj] 。
* 对于第 j 个查询,如果单元格 [rowj, colj] 是被照亮的,则查询结果为 1 ,否则为 0 。
* 在第 j 次查询之后 [按照查询的顺序] ,关闭 位于单元格 grid[rowj][colj] 上及相邻 8 个方向上(与单元格 grid[rowi][coli] 共享角或边)的任何灯。
*
* 返回一个整数数组 ans 作为答案, ans[j] 应等于第 j 次查询 queries[j] 的结果,1 表示照亮,0 表示未照亮。
*/
public class GridIllumination {
public static void main(String[] args) {
GridIllumination g = new GridIllumination();
System.out.println(Arrays.toString(g.gridIllumination(5, new int[][]{{0, 0}, {4, 4}}, new int[][]{{1, 1}, {1, 0}})));
}
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
Map<Integer, Integer> row = new HashMap<>();
Map<Integer, Integer> column = new HashMap<>();
Map<Integer, Integer> diagonal = new HashMap<>();
Map<Integer, Integer> antiDiagonal = new HashMap<>();
Set<Long> points = new HashSet<>();
// 遍历所有打开的灯
for (int[] lamp : lamps) {
// 同一盏灯不重复添加
if (!points.add(hash(lamp[0], lamp[1]))) {
continue;
}
row.put(lamp[0], row.getOrDefault(lamp[0], 0) + 1);
column.put(lamp[1], column.getOrDefault(lamp[1], 0) + 1);
diagonal.put(lamp[0] - lamp[1], diagonal.getOrDefault(lamp[0] - lamp[1], 0) + 1);
antiDiagonal.put(lamp[0] + lamp[1], antiDiagonal.getOrDefault(lamp[0] + lamp[1], 0) + 1);
}
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int r = queries[i][0];
int c = queries[i][1];
if (row.getOrDefault(r, 0) > 0) {
result[i] = 1;
} else if (column.getOrDefault(c, 0) > 0) {
result[i] = 1;
} else if (diagonal.getOrDefault(r - c, 0) > 0) {
result[i] = 1;
} else if (antiDiagonal.getOrDefault(r + c, 0) > 0) {
result[i] = 1;
}
for (int x = r - 1; x <= r + 1; x++) {
for (int y = c - 1; y <= c + 1; y++) {
if (x < 0 || y < 0 || x >= n || y >= n) {
continue;
}
if (points.remove(hash(x, y))) {
row.put(x, row.get(x) - 1);
if (row.get(x) == 0) {
row.remove(x);
}
column.put(y, column.get(y) - 1);
if (column.get(y) == 0) {
column.remove(y);
}
diagonal.put(x - y, diagonal.get(x - y) - 1);
if (diagonal.get(x - y) == 0) {
diagonal.remove(x - y);
}
antiDiagonal.put(x + y, antiDiagonal.get(x + y) - 1);
if (antiDiagonal.get(x + y) == 0) {
antiDiagonal.remove(x + y);
}
}
}
}
}
return result;
}
private long hash(int x, int y) {
return (long) x + ((long) y << 32);
}
}