Leetcode Problem 1222. Queens That Can Attack the King

1222. Queens That Can Attack the King

Leetcode Solutions

Traverse in Directions from King

  1. Create a set of tuples for all queen positions for constant time lookup.
  2. Initialize a result list to store the positions of attacking queens.
  3. Define the 8 possible directions in which a queen can attack.
  4. For each direction, start from the king's position and move step by step in that direction.
  5. If a queen is encountered, add her position to the result list and break the loop for that direction.
  6. If the edge of the board is reached, move to the next direction.
  7. Return the result list.
UML Thumbnail

Check Each Queen's Attack Range

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...