Created
March 12, 2018 22:34
-
-
Save jianminchen/3dec8ea6ed5c25cccfff08cb962df84f to your computer and use it in GitHub Desktop.
Leetcode 688 - analysis of knight probability in chessboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Leetcode 688 - Knight probability chessboard | |
I do not really understand the algorithm until I read the blog: | |
http://zxi.mytechroad.com/blog/dynamic-programming/688-knight-probability-in-chessboard/ | |
Here is the analysis to help me understand the algorithm: | |
How to think about the algorithm using dynamic programming? We first have to define variables, kSteps, row, col, | |
dp[kSteps][row][col] := number of ways to move to (col, row) after k steps. | |
dp[kSteps][row][col] = sum(dp[kSteps - 1][y][x]), for all (x,y) can move to (col, row). | |
answer = dum(dp[kSteps][row][col] / (8^kSteps) | |
Time complexity: O(k*N^2) | |
Space complexity: O(k*N^2) -> O(N^2) | |
Let us go over an example using N = 3, K = 2, r = 0, c = 0 | |
* * * * * * 2 * 1 | |
* * * => * * 1 => * * 0 | |
* * * * 1 * 1 0 * | |
So using the above steps, we can tell that the probability is (2+1+1)/ (8^2) = 0.0625 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment