#1222

Queens That Can Attack the King

pupil · 540 · lc medium +28 · verified · 72.6% accepted · 1,004 likes · top 82%

Description

On a standard 0-indexed 8x8 chessboard, several black queens and one white king are placed.

You are given queens where queens[i] = [xQueeni, yQueeni] gives each queen's position, and king = [xKing, yKing] gives the king's position.

A queen can directly attack the king if they share the same row, column, or diagonal with no other queen blocking between them.

Return the positions of all queens that can directly attack the king. Order does not matter.

Example 1:

Input: queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
Output: [[0,1],[1,0],[3,3]]
Explanation: The diagram above shows the three queens that can directly attack the king and the three queens that cannot attack the king (i.e., marked with red dashes).

Example 2:

Input: queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
Output: [[2,2],[3,4],[4,4]]
Explanation: The diagram above shows the three queens that can directly attack the king and the three queens that cannot attack the king (i.e., marked with red dashes).

Code

1
2
3