The dynamic programming (dp) is one of the most intriguing realm in algorithm. The main purpose for dp is to avoid redundant calculations. One of the keys to achieve this is to solve the same problem with a smaller size. Once the same question with the smaller size is solved, the results can be stored and re-used directly when meet with the same problem again.
The populate word for dp is state, which means exactly the same question but different sizes (so the real idea behind dp is recursion, but with memoization). The state essentially is an abstract of the question to be solved. How to correctly define the state is one of two main difficulties for dp.
The correct state means:
1. the state can fully describe the question to be solved;
2. the state transition formula can be derived based on the state definition.
The other difficulty is initialization. The initial states usually mean the question with very smaller sizes, so the results can be obtained without complex calculations. However, it turns out that the initialization depends on the definition of the state, which is highly error-prone.
To sum up the key points for the dp:
1. how to define the state & state transition formula;
2. how to initialize the states which is the starting point of the dp.
Interview Questions
Comments
Post a Comment