Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Knight Probability in Chessboard

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "688" 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 an n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1). A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction. Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there. The knight continues moving until it has made exactly k moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving. Example 1: Input: n = 3, k = 2, row = 0, column = 0 Output: 0.06250 Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board. From each of those positions, there are also two moves that will keep the knight on the board. The total probability the knight stays on the board is 0.0625. Example 2: Input: n = 1, k = 0, row = 0, column = 0 Output: 1.00000 Constraints: 1 <= n <= 25 0 <= k <= 100 0 <= row, column <= n - 1

Explanation

Here's a breakdown of the solution:

  • Dynamic Programming: Use dynamic programming to store the probability of the knight being at each cell after each move.
  • Iterative Calculation: Iterate through the number of moves, updating the probabilities based on the possible knight moves.
  • Base Case and Accumulation: Initialize the starting cell with probability 1.0 at move 0. Accumulate the probabilities of all cells on the board after k moves.

  • Time Complexity: O(k n^2), where k is the number of moves and n is the board size. *Space Complexity: O(n^2).

Code

    def knightProbability(n: int, k: int, row: int, column: int) -> float:
    """
    Calculates the probability that a knight remains on an n x n chessboard after k moves,
    starting from cell (row, column).
    """

    dp = [[[0.0] * n for _ in range(n)] for _ in range(k + 1)]
    dp[0][row][column] = 1.0

    moves = [
        (-2, -1), (-2, 1), (-1, -2), (-1, 2),
        (1, -2), (1, 2), (2, -1), (2, 1)
    ]

    for move_count in range(1, k + 1):
        for i in range(n):
            for j in range(n):
                for dr, dc in moves:
                    prev_row, prev_col = i - dr, j - dc
                    if 0 <= prev_row < n and 0 <= prev_col < n:
                        dp[move_count][i][j] += dp[move_count - 1][prev_row][prev_col] / 8.0

    total_probability = 0.0
    for i in range(n):
        for j in range(n):
            total_probability += dp[k][i][j]

    return total_probability

More from this blog

C

Chatmagic blog

2894 posts