On a 0-indexed 8 x 8 chessboard, there can be multiple black queens and one white king.
You are given a 2D integer array queens where queens[i] = [xQueeni, yQueeni] represents the position of the ith black queen on the chessboard. You are also given an integer array king of length 2 where king = [xKing, yKing] represents the position of the white king.
Return the coordinates of the black queens that can directly attack the king. You may return the answer in any order.
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).
Constraints:
1 <= queens.length < 64queens[i].length == king.length == 20 <= xQueeni, yQueeni, xKing, yKing < 8When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find all the queens that can attack the king on a chessboard. The brute force approach simply checks every queen to see if it can attack the king, without any shortcuts.
Here's how the algorithm would work step-by-step:
def queens_that_can_attack_the_king(queens, king):
attacking_queens = []
king_row = king[0]
king_column = king[1]
for queen in queens:
queen_row = queen[0]
queen_column = queen[1]
# Check if the queen is in the same row as the king
if queen_row == king_row:
attacking_queens.append(queen)
continue
# Check if the queen is in the same column as the king
if queen_column == king_column:
attacking_queens.append(queen)
continue
# Check if the queen is on the same diagonal as the king
if abs(queen_row - king_row) == abs(queen_column - king_column):
attacking_queens.append(queen)
return attacking_queensThe most efficient way to solve this puzzle is to check along the eight possible directions from the king until we find a queen. We stop searching in a direction once a queen is found because any further pieces in that direction are irrelevant.
Here's how the algorithm would work step-by-step:
def queens_that_can_attack_the_king(queens, king):
attacking_queens = []
king_row, king_col = king
# Define the 8 directions to check
directions = [
(-1, 0), # Up
(1, 0), # Down
(0, -1), # Left
(0, 1), # Right
(-1, -1), # Top-Left
(-1, 1), # Top-Right
(1, -1), # Bottom-Left
(1, 1) # Bottom-Right
]
for row_direction, col_direction in directions:
closest_queen = None
min_distance = float('inf')
# Iterate through each queen to find the closest one in current direction
for queen_row, queen_col in queens:
if row_direction == 0 and queen_row != king_row:
continue
if col_direction == 0 and queen_col != king_col:
continue
if row_direction != 0 and col_direction != 0 and (queen_row - king_row) * col_direction != (queen_col - king_col) * row_direction:
continue
row_distance = queen_row - king_row
col_distance = queen_col - king_col
# Ensure the queen is in the correct direction
if row_direction != 0 and row_distance * row_direction <= 0:
continue
if col_direction != 0 and col_distance * col_direction <= 0:
continue
distance = abs(row_distance) + abs(col_distance)
# Check if this queen is closer than the current closest
if distance < min_distance:
min_distance = distance
closest_queen = (queen_row, queen_col)
# If a closest queen was found, add it to the result
if closest_queen:
attacking_queens.append(closest_queen)
return attacking_queens
# Finds the queens closest to the king in each of the 8 directions.| Case | How to Handle |
|---|---|
| Null or empty queens list | Return an empty list immediately, as no queens can attack. |
| Null king position | Throw an IllegalArgumentException or return an empty list, as a valid king position is required. |
| King and queen at the same position | The queen can attack, so it should be included in the result. |
| Multiple queens on the same line of sight to the king | Only the closest queen to the king on each line of sight should be considered as attacking. |
| Queen positions outside the chessboard boundaries (0-7) | Ignore any queen with invalid coordinates (outside the 0-7 range) or throw an IllegalArgumentException. |
| King position outside the chessboard boundaries (0-7) | Throw an IllegalArgumentException as the king must be within the board. |
| Large number of queens; assess time complexity. | The solution should have a time complexity of O(N) where N is the number of queens to avoid timeouts with large input. |
| No queens can attack the king | The algorithm should correctly return an empty list in this scenario. |