Skip to content
LC-0688 Medium LeetCode

688. Knight Probability in Chessboard

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 57% Topics: Dynamic Programming
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(k * n^2)
# Space: O(n^2)

class Solution(object):
    def knightProbability(self, N, K, r, c):
        """
        :type N: int
        :type K: int
        :type r: int
        :type c: int
        :rtype: float
        """
        directions = \
            [[ 1, 2], [ 1, -2], [ 2, 1], [ 2, -1], \
             [-1, 2], [-1, -2], [-2, 1], [-2, -1]]
        dp = [[[1 for _ in xrange(N)] for _ in xrange(N)] for _ in xrange(2)]
        for step in xrange(1, K+1):
            for i in xrange(N):
                for j in xrange(N):
                    dp[step%2][i][j] = 0
                    for direction in directions:
                        rr, cc = i+direction[0], j+direction[1]
                        if 0 <= cc < N and 0 <= rr < N:
                            dp[step%2][i][j] += 0.125 * dp[(step-1)%2][rr][cc]

        return dp[K%2][r][c]

Solution from kamyu104/LeetCode-Solutions · MIT