Count warehouse robots boxed in on all four sides
I was asked this
No reports yet. Sign in to say where you were asked it.
A warehouse floor is modeled as an integer grid. Each delivery robot sits at a point (x[i], y[i]), and no two robots share a point.
A robot is idle when there is at least one other robot:
- above it (same
x, largery), - below it (same
x, smallery), - to its left (same
y, smallerx), and - to its right (same
y, largerx).
The other robots can be any distance away; they only need to share the row or column.
Implement:
def count_idle_drives(x: list[int], y: list[int]) -> int: ...
Return the number of idle robots.
Constraints
-
0 <= n <= 10^5, wheren = len(x) == len(y) -10^9 <= x[i], y[i] <= 10^9- All points are distinct.
Example 1
x = [0, 0, 0, 1, 1, 1, 2, 2, 2]
y = [0, 1, 2, 0, 1, 2, 0, 1, 2]
-> 1
The robots fill a 3x3 square. Only the centre (1, 1) has robots on all four sides.
Example 2
x = [5, 5, 5, 2, 9]
y = [0, 4, 8, 4, 4]
-> 1
The robot at (5, 4) has (5, 8) above, (5, 0) below, (2, 4) to the left and (9, 4) to the right. None of them is adjacent, but they still count. Every other robot is missing at least one side.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
