Skip to content

Commit 2ed2bab

Browse files
committed
Add solution #1001
1 parent 1961f04 commit 2ed2bab

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,079 LeetCode solutions in JavaScript
1+
# 1,080 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -809,6 +809,7 @@
809809
998|[Maximum Binary Tree II](./solutions/0998-maximum-binary-tree-ii.js)|Medium|
810810
999|[Available Captures for Rook](./solutions/0999-available-captures-for-rook.js)|Easy|
811811
1000|[Minimum Cost to Merge Stones](./solutions/1000-minimum-cost-to-merge-stones.js)|Hard|
812+
1001|[Grid Illumination](./solutions/1001-grid-illumination.js)|Hard|
812813
1002|[Find Common Characters](./solutions/1002-find-common-characters.js)|Easy|
813814
1004|[Max Consecutive Ones III](./solutions/1004-max-consecutive-ones-iii.js)|Medium|
814815
1005|[Maximize Sum Of Array After K Negations](./solutions/1005-maximize-sum-of-array-after-k-negations.js)|Easy|

solutions/1001-grid-illumination.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* 1001. Grid Illumination
3+
* https://leetcode.com/problems/grid-illumination/
4+
* Difficulty: Hard
5+
*
6+
* There is a 2D grid of size n x n where each cell of this grid has a lamp that is initially
7+
* turned off.
8+
*
9+
* You are given a 2D array of lamp positions lamps, where lamps[i] = [rowi, coli] indicates that
10+
* the lamp at grid[rowi][coli] is turned on. Even if the same lamp is listed more than once, it
11+
* is turned on.
12+
*
13+
* When a lamp is turned on, it illuminates its cell and all other cells in the same row, column,
14+
* or diagonal.
15+
*
16+
* You are also given another 2D array queries, where queries[j] = [rowj, colj]. For the jth query,
17+
* determine whether grid[rowj][colj] is illuminated or not. After answering the jth query, turn off
18+
* the lamp at grid[rowj][colj] and its 8 adjacent lamps if they exist. A lamp is adjacent if its
19+
* cell shares either a side or corner with grid[rowj][colj].
20+
*
21+
* Return an array of integers ans, where ans[j] should be 1 if the cell in the jth query was
22+
* illuminated, or 0 if the lamp was not.
23+
*/
24+
25+
/**
26+
* @param {number} n
27+
* @param {number[][]} lamps
28+
* @param {number[][]} queries
29+
* @return {number[]}
30+
*/
31+
var gridIllumination = function(n, lamps, queries) {
32+
const rowCount = new Map();
33+
const colCount = new Map();
34+
const diag1Count = new Map();
35+
const diag2Count = new Map();
36+
const lampSet = new Set();
37+
38+
for (const [r, c] of lamps) {
39+
if (!lampSet.has(`${r},${c}`)) {
40+
lampSet.add(`${r},${c}`);
41+
rowCount.set(r, (rowCount.get(r) || 0) + 1);
42+
colCount.set(c, (colCount.get(c) || 0) + 1);
43+
diag1Count.set(r - c, (diag1Count.get(r - c) || 0) + 1);
44+
diag2Count.set(r + c, (diag2Count.get(r + c) || 0) + 1);
45+
}
46+
}
47+
48+
const directions = [[0, 0], [0, 1], [0, -1], [1, 0], [1, 1], [1, -1], [-1, 0], [-1, 1], [-1, -1]];
49+
const result = new Array(queries.length);
50+
51+
for (let i = 0; i < queries.length; i++) {
52+
const [r, c] = queries[i];
53+
result[i] = (rowCount.get(r) > 0 || colCount.get(c) > 0
54+
|| diag1Count.get(r - c) > 0 || diag2Count.get(r + c) > 0) ? 1 : 0;
55+
56+
for (const [dr, dc] of directions) {
57+
const newR = r + dr;
58+
const newC = c + dc;
59+
const key = `${newR},${newC}`;
60+
61+
if (newR >= 0 && newR < n && newC >= 0 && newC < n && lampSet.has(key)) {
62+
lampSet.delete(key);
63+
rowCount.set(newR, rowCount.get(newR) - 1);
64+
colCount.set(newC, colCount.get(newC) - 1);
65+
diag1Count.set(newR - newC, diag1Count.get(newR - newC) - 1);
66+
diag2Count.set(newR + newC, diag2Count.get(newR + newC) - 1);
67+
}
68+
}
69+
}
70+
71+
return result;
72+
};

0 commit comments

Comments
 (0)