↓Skip to main content
  1. LeetCode/

LeetCode 1222: Queens That Can Attack the King

·2 mins· ·
LeetCode Medium Array Matrix Simulation
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-30

  • Submissions: Accepted ×2

Approach
#

Work backward from the king (Array Template 5): walk each of the eight directions once; the first queen met can attack directly and anything beyond is blocked. Each direction takes at most 7 steps.

My Solution
#

class Solution:
    def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
        qs = set(map(tuple, queens))
        res = []
        for dx in (-1, 0, 1):
            for dy in (-1, 0, 1):
                if dx == dy == 0:
                    continue
                x, y = king[0] + dx, king[1] + dy
                while 0 <= x < 8 and 0 <= y < 8:
                    if (x, y) in qs:
                        res.append([x, y])
                        break
                    x, y = x + dx, y + dy
        return res

I originally checked [x, y] in queens on a list; the second submission replaced a chain of if i == -1 with adding the direction directly. The board is tiny, so both were fast; here it uses a set.

Complexity
#

Time O(q + 8 × 8), space O(q).

Mistakes to Avoid
#

  • Stop at the first queen in each direction; the rest are blocked.

Interview Explanation
#

I walk out from the king in eight directions; the first queen in each can attack and the rest are blocked. Queens go in a set for O(1) lookups. Time O(q + 64).

Related

LeetCode 54: Spiral Matrix
·2 mins
LeetCode Medium Array Matrix Simulation
LeetCode 54 note: keep four boundaries and walk right, down, left and up, shrinking a boundary after each side
LeetCode 59: Spiral Matrix II
·2 mins
LeetCode Medium Array Matrix Simulation
LeetCode 59 note: keep four boundaries as in LC 54, but fill in 1 to n² instead of reading
LeetCode 3494: Find the Minimum Amount of Time to Brew Potions
·2 mins
LeetCode Medium Simulation Prefix-Sum Array
LeetCode 3494 note: for each potion, push forward to get the last wizard’s finish time (everyone must be free), then subtract backward to get each wizard’s finish time
LeetCode 735: Asteroid Collision
·2 mins
LeetCode Medium Array Stack Simulation
LeetCode 735 note: scan left to right with a stack; only a right-moving top meeting a left-moving asteroid collides, the smaller explodes and equal sizes both do
LeetCode 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·2 mins
LeetCode Medium Array Matrix Prefix-Sum
LeetCode 3070 note: submatrices containing the top-left corner are exactly the 2D prefix sums P[i][j]; count those <= k
LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid
·2 mins
LeetCode Medium Array Hash-Table Matrix Counting
LeetCode 3071 note: count each value on and off the Y, enumerate the Y value a and background value b (a != b), and take the fewest cells to change