Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Valid Move Combinations On Chessboard

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2056" 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

There is an 8 x 8 chessboard containing n pieces (rooks, queens, or bishops). You are given a string array pieces of length n, where pieces[i] describes the type (rook, queen, or bishop) of the ith piece. In addition, you are given a 2D integer array positions also of length n, where positions[i] = [ri, ci] indicates that the ith piece is currently at the 1-based coordinate (ri, ci) on the chessboard. When making a move for a piece, you choose a destination square that the piece will travel toward and stop on. A rook can only travel horizontally or vertically from (r, c) to the direction of (r+1, c), (r-1, c), (r, c+1), or (r, c-1). A queen can only travel horizontally, vertically, or diagonally from (r, c) to the direction of (r+1, c), (r-1, c), (r, c+1), (r, c-1), (r+1, c+1), (r+1, c-1), (r-1, c+1), (r-1, c-1). A bishop can only travel diagonally from (r, c) to the direction of (r+1, c+1), (r+1, c-1), (r-1, c+1), (r-1, c-1). You must make a move for every piece on the board simultaneously. A move combination consists of all the moves performed on all the given pieces. Every second, each piece will instantaneously travel one square towards their destination if they are not already at it. All pieces start traveling at the 0th second. A move combination is invalid if, at a given time, two or more pieces occupy the same square. Return the number of valid move combinations​​​​​. Notes: No two pieces will start in the same square. You may choose the square a piece is already on as its destination. If two pieces are directly adjacent to each other, it is valid for them to move past each other and swap positions in one second. Example 1: Input: pieces = ["rook"], positions = [[1,1]] Output: 15 Explanation: The image above shows the possible squares the piece can move to. Example 2: Input: pieces = ["queen"], positions = [[1,1]] Output: 22 Explanation: The image above shows the possible squares the piece can move to. Example 3: Input: pieces = ["bishop"], positions = [[4,3]] Output: 12 Explanation: The image above shows the possible squares the piece can move to. Constraints: n == pieces.length n == positions.length 1 <= n <= 4 pieces only contains the strings "rook", "queen", and "bishop". There will be at most one queen on the chessboard. 1 <= ri, ci <= 8 Each positions[i] is distinct.

Explanation

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

  • High-Level Approach:

    • Iterate through all possible destination square combinations for the pieces. Since each piece can move to any of the 64 squares, and we have at most 4 pieces, we iterate through the cartesian product of all possible squares for each piece.
    • For each destination combination, simulate the moves of all pieces at each second, and check for collisions.
    • If no collision occurs during the simulation, increment the valid move combination count.
  • Complexity:

    • Runtime: O(64n * T), where n is the number of pieces and T is the maximum time it takes for a piece to reach its destination (worst case is the length of chessboard).
    • Storage: O(n), primarily for storing the positions and destinations of the pieces.

Code

    def solve():
    pieces = ["rook"]
    positions = [[1, 1]]
    print(count_valid_move_combinations(pieces, positions))

    pieces = ["queen"]
    positions = [[1, 1]]
    print(count_valid_move_combinations(pieces, positions))

    pieces = ["bishop"]
    positions = [[4, 3]]
    print(count_valid_move_combinations(pieces, positions))

def count_valid_move_combinations(pieces, positions):
    n = len(pieces)
    destinations = []
    for _ in range(n):
        destinations.append([])
        for r in range(1, 9):
            for c in range(1, 9):
                destinations[-1].append((r, c))

    import itertools
    all_destination_combinations = itertools.product(*destinations)

    valid_combinations = 0

    for destination_combination in all_destination_combinations:
        valid = True

        max_time = 0
        for i in range(n):
            r1, c1 = positions[i]
            r2, c2 = destination_combination[i]
            max_time = max(max_time, max(abs(r1 - r2), abs(c1 - c2)))

        for t in range(max_time + 1):
            occupied = {}
            for i in range(n):
                r1, c1 = positions[i]
                r2, c2 = destination_combination[i]

                current_r, current_c = r1, c1

                if r1 != r2 or c1 != c2:
                    time_to_reach = max(abs(r1 - r2), abs(c1 - c2))

                    if t >= time_to_reach:
                      current_r, current_c = r2, c2
                    else:

                      dr = r2 - r1
                      dc = c2 - c1

                      move_r = dr / time_to_reach if time_to_reach > 0 else 0
                      move_c = dc / time_to_reach if time_to_reach > 0 else 0

                      current_r = round(r1 + move_r * t)
                      current_c = round(c1 + move_c * t)


                if (current_r, current_c) in occupied:
                    valid = False
                    break
                else:
                    occupied[(current_r, current_c)] = 1
            if not valid:
                break

        if valid:
            valid_combinations += 1

    return valid_combinations

solve()

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Number of Valid Move Combinations On Chessboard