Count queen attacks on points with blockers
You are given positions on an unbounded 2D integer grid.
queens: A list of (x, y) coordinates representing queen positions.points: A list of (x, y) coordinates representing query points.
A queen attacks along 8 directions: horizontal, vertical, and the two main diagonals.
Part 1: No Blockers
For each point p in points, compute the total number of queens that can attack p. A queen at q attacks p if p lies on the same row, column, or diagonal as q.
Return an array ans where ans[i] is the count of queens attacking points[i].
Part 2: Blockers (Rocks)
In addition to the above, you are now also given:
rocks: A list of (x, y) coordinates representing rock positions.
Rocks block attacks. A queen at q attacks a point p only if there is no rock strictly between q and p along the line of attack.
Again, return an array ans where ans[i] is the count of queens attacking points[i] under these new rules.
Input/Output
- Input:
queens,points, and optionallyrocks(for Part 2). Coordinates are 2-element arrays or similar structures. - Output: An integer array of size
len(points), whereans[i]corresponds topoints[i].
Constraints
1 <= len(queens), len(points), len(rocks) <= 2 * 10^5- Coordinates fit within 32-bit signed integers.
- The target time complexity for Part 2 is approximately
O((Q + R + P) log(Q+R)), where Q, R, P are the lengths ofqueens,rocks, andpointsrespectively.
AI interview prep
Core dilemma
Efficiently performing numerous line-of-sight queries in a dense 2D grid, where blocking objects (rocks) dynamically alter attack paths, requiring spatial indexing and careful handling of proximity.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Count queen attacks on points with blockers
You are given positions on an unbounded 2D integer grid.
queens: A list of (x, y) coordinates representing queen positions.points: A list of (x, y) coordinates representing query points.
A queen attacks along 8 directions: horizontal, vertical, and the two main diagonals.
Part 1: No Blockers
For each point p in points, compute the total number of queens that can attack p. A queen at q attacks p if p lies on the same row, column, or diagonal as q.
Return an array ans where ans[i] is the count of queens attacking points[i].
Part 2: Blockers (Rocks)
In addition to the above, you are now also given:
rocks: A list of (x, y) coordinates representing rock positions.
Rocks block attacks. A queen at q attacks a point p only if there is no rock strictly between q and p along the line of attack.
Again, return an array ans where ans[i] is the count of queens attacking points[i] under these new rules.
Input/Output
- Input:
queens,points, and optionallyrocks(for Part 2). Coordinates are 2-element arrays or similar structures. - Output: An integer array of size
len(points), whereans[i]corresponds topoints[i].
Constraints
1 <= len(queens), len(points), len(rocks) <= 2 * 10^5- Coordinates fit within 32-bit signed integers.
- The target time complexity for Part 2 is approximately
O((Q + R + P) log(Q+R)), where Q, R, P are the lengths ofqueens,rocks, andpointsrespectively.