Advertisement

Queens That Can Attack the King - LeetCode 1222 Solution

Queens That Can Attack the King - Complete Solution Guide

Queens That Can Attack the King is LeetCode problem 1222, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] = [xQueen i , yQueen i ] represents the position of the i th 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: qu

Detailed Explanation

The problem asks us to find all black queens on an 8x8 chessboard that can directly attack a white king. A queen can attack horizontally, vertically, and diagonally. We are given the positions of the queens and the king as integer coordinates. The goal is to return a list of the queen coordinates that can attack the king, ensuring that no other queen or obstacle blocks the line of sight between the queen and the king.

Solution Approach

The provided solution iterates through the 8 possible directions from the king's position. For each direction, it moves one step at a time until it finds a queen or reaches the edge of the board. If a queen is found, its coordinates are added to the result list. Using a set/hash table to store the queen positions allows for quick checking if a given coordinate contains a queen.

Step-by-Step Algorithm

  1. Step 1: Create a set (or hash table) to store the positions of the queens for fast lookup.
  2. Step 2: Initialize an empty list to store the attacking queens.
  3. Step 3: Define the eight possible directions a queen can attack in: up, down, left, right, and the four diagonals.
  4. Step 4: Iterate through each of the eight directions.
  5. Step 5: For each direction, start from the king's position and move one step at a time.
  6. Step 6: Check if the current position is within the bounds of the chessboard (0 to 7).
  7. Step 7: If the current position is within bounds, check if there's a queen at that position using the set.
  8. Step 8: If a queen is found, add its coordinates to the list of attacking queens and stop searching in that direction.
  9. Step 9: If the current position is out of bounds, stop searching in that direction.
  10. Step 10: After iterating through all directions, return the list of attacking queens.

Key Insights

  • Insight 1: We need to check all 8 possible directions (horizontal, vertical, and diagonals) from the king's position.
  • Insight 2: A set or hash table is an efficient way to store the queen positions for fast lookup.
  • Insight 3: We should iterate through each direction until we either encounter a queen or reach the edge of the board.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(N)

Topics

This problem involves: Array, Matrix, Simulation.

Companies

Asked at: Media.net.