Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Queens That Can Attack the King

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1222" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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] = [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 < 64 queens[i].length == king.length == 2 0 <= xQueeni, yQueeni, xKing, yKing < 8 All the given positions are unique.

Explanation

Here's a breakdown of the approach, complexity, and the Python code:

  • Approach: The core idea is to iterate through the eight possible directions (horizontally, vertically, and diagonally) from the king's position. For each direction, find the nearest queen that lies in that line of sight.

  • Efficiency: Instead of checking every queen for each direction, we can process queens as we iterate each direction. This reduces unnecessary comparisons.

  • Complexity:

    • Runtime: O(N), where N is the number of queens. In the worst case, we might iterate through all queens in specific directions.
    • Storage: O(1). Although the output array grows linearly with the number of attacking queens, the auxiliary space required by the algorithm itself is constant.

Code

    def queensAttacktheKing(queens, king):
    """
    Finds the black queens that can directly attack the white king on an 8x8 chessboard.

    Args:
        queens (list of list): A list of queen coordinates [[xQueeni, yQueeni]].
        king (list): The king's coordinates [xKing, yKing].

    Returns:
        list of list: A list of attacking queen coordinates.
    """
    directions = [
        (0, 1),  # right
        (0, -1),  # left
        (1, 0),  # down
        (-1, 0),  # up
        (1, 1),  # down-right
        (1, -1),  # down-left
        (-1, 1),  # up-right
        (-1, -1),  # up-left
    ]

    attacking_queens = []
    for dx, dy in directions:
        x, y = king[0] + dx, king[1] + dy
        closest_queen = None

        while 0 <= x < 8 and 0 <= y < 8:
            for queen in queens:
                if queen[0] == x and queen[1] == y:
                    closest_queen = queen
                    break  # Found queen in this direction

            if closest_queen:
                attacking_queens.append(closest_queen)
                break  # Move to next direction
            x += dx
            y += dy

    return attacking_queens

More from this blog

C

Chatmagic blog

2894 posts