Showing posts with label Leetcode 688: Knight probability in chessboard. Show all posts
Showing posts with label Leetcode 688: Knight probability in chessboard. Show all posts

Thursday, March 15, 2018

Build a new drill for daily workout

March 15, 2018


Introduction


It is easy to stay in the comfortable zone. Specially when I work on the algorithm Knight's tour today, I feel so uncomfortable, I have to deal with an unknown problem, draw a graph, think about problem space, counting sort, seek tips, one after the another. Even though I know that I have to simplify the problem to make it straightforward one. It is called uncomfortable feeling.

In order to push myself out of comfortable zone, I have to develop a drill for myself, work on a new algorithm every few hours in the day time.

How to develop the drill?


I chatted one of computer science graduate student, he just got Google intern offer. He told me that he has worked on Leetcode over 400 algorithms. What I have worked on, less than 150 algorithm from Leetcode.

It is so easy to work on one algorithm. If I take a break every hour, I may just take five minutes break. Read the problem statement of the algorithm using 5 minutes. That is it.

Whenever I have time, I will let myself think about the algorithm.


Need more curiosity?


I need to do some short research how to develop more curiosity on algorithm and data structure. Please list three ideas here.



Saturday, March 10, 2018

688. Knight Probability in Chessboard

March 10, 2018

Introduction


Dynamic programming algorithm is more advanced compared to recursive function, depth first search algorithm. I like to write down some note to document my practice, time spent is longer than three hours.


Algorithm practice


I did read one blog to document the analysis of the algorithm with the code, so I went over analysis word by word, and organized the idea first and put into the gist. But I could not understand the algorithm.

So I continued to search more blogs to read, and then I found one with the graph to explain three dimension dynamic programming. I understood the idea to apply dynamic programming algorithm.

Next I worked on code from discussion panel and then converted to the C# code. But it still took me more than one hour, I had to debug the code and pass the failed test case.

It is so interesting to learn the algorithm by going over the steps. It is not difficult but it took me a few hours. I think that it should take less than one hour in total.

Here is the gist I created for understanding the dynamic programming algorithm after I read a few coding blog and one discussion on Leetcode.com.

Here is the code to implement in two dimension array instead of 3 dimension, since we do not need to save all intermediate steps.

Here is the C# code to pass all test cases on Leetcode online judge 688.