I'm currently trying to solve a problem I failed to solve in a white-board mock interview. I got stuck a few times. Any help is appreciated.
The question was worded as such:
Given an NxN grid with an array of lamp coordinates. Each lamp provides illumination to every square on their x-axis, every square on their y-axis, and every square that lies in their diagonal (think of the Queen in chess). Given an array of query coordinates, determine whether that point is illuminated or not.
The catch is when checking a query, all lamps adjacent to or on that query gets turned off. If you visit a coordinate/cell, turn off all lamps that are in that coordinates or adjacent. Two cells are adjacent if they share the same edge or corner.
- write a function
checkLampIllumination(N, lamps, queries)N: size of the gridlamps: coordinates of a lampqueries: coordinates on the grid to be checked if they are lit or not
The test case I was given was:
N = 8
lamps = [
[1,6],
[5,6],
[7,3],
[3,2]
]
queries = [
[4,4],
[6,6],
[8,1],
[3,2],
[2,3]
]
OUTPUT:
['DARK','LIGHT','DARK','DARK','LIGHT']
Second Test case:
checkLampIllumination(8, [[4,3],[4,4]], [[3,4],[7,6]])
N = 8
lamps = [
[4,3],
[4,4]
]
queries = [
[3,4],
[7,6]
]
OUTPUT:
['DARK','LIGHT']
Here is my current stab at it. I think the current solution just creates the grid. I really don't know where to go from here.
const checkLampIllumination=(N, lamps, queries) => {
var gridNxN = []
var row = []
for (var i = 1; i < 100; i++) {
if (i.toString().indexOf('0') !== -1) {
row.push(i)
gridNxN.push(row)
row = []
} else {
row.push(i)
}
}
}