|
| 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