I will be honest, this is a task, but I just cannot find a solution for it. Please remember that I am not asking for an answer, but just a guide.
Problem: Create an algorithm that works in O (n) time (linear), which can find one suspicious house (point) in a 2D grid. This is suspicious if it consumes equal or greater electrical consumption of two vertical and two horizontal neighbors. Note: It just requires a suspicious house to return.
Solution: Iβm not even sure how to reach such a solution. If you check n houses, you can also check your four neighboring neighbors. 4n / n ^ 2, which simplifies to 4 / n. This means that as the grid expands, it is less likely to find a suspicious house.
What I tried: - Various data structures (most of them are nlogn) - Folding the grid (again nlogn)
Thanks in advance.
Edit:
My mistake: the grid (nxn) makes the number of houses n ^ 2, sorry for the confusion.
Edit2:
This question is for sure, maybe I'm reading it wrong?
Police are looking for homes that have especially large tritium. To simplify the problem, imagine that they are exploring houses that are located on an n Γ n grid. Each house on the grid has some electricity consumption, e (i, j). Police consider a house suspicious if it has electricity consumption equal to or greater than each of its vertical and horizontal neighbors. Develop an algorithm that works in O (n) time and returns the location of a suspicious house.