By Zhenyu from Cainiao Network
First, let's look at the official definition of dynamic planning:
A dp array is used to store the intermediate state of the calculation, thus avoiding double calculation. (Overlapping subproblems)
The following example is used to introduce the basic concepts of dynamic planning. Now, there are enough 1 CNY, 2 CNY, and 5 CNY coins, and we need to gather n yuan by finding the minimum number of coins used. Assuming that n is 10099, the mapping analysis is listed below:
The figure shows that dp[10097] is calculated twice repeatedly. When the number is smaller, it is calculated more times repeatedly. If you use a memo array to record, you can avoid double calculations, which is the core of dynamic planning algorithms that can improve computational efficiency by trading space for time. Therefore, a one-dimensional array dp can be defined to store the intermediate state of the calculation, where dp(i) represents the least number of coins used to gather i elements. That is the memo.
dp = [f(0), f(1), ...,f(10097),…] where f(o) indicates the initial state. From the figure, it can be obtained that dp[10099] = min(dp[10098] , dp[10097] , dp[10094]) +1 can be inferred: f(n) = Math.min(f(n-1]), f(n-2), f(n-5)) +1
The preceding formula is the current state transition equation in dynamic planning. In summary, dynamic planning DP = recursive plus cache.
The current difficulties of dynamic planning are mainly reflected in the following two aspects:
In particular, there is no fixed formula to solve the state transition equation, it can only be obtained from the analysis of the problem.
The problems of dynamic planning can be solved in the following four steps:
First, draw and analyze example 1, as shown in the following figure:
After that, consider adding a number X after 12. When X takes 0, it is very special because 0 cannot be transcoded into any letter. It can only be combined with the previous number, as shown in the following figure:
At this time, the 2 before X is not representative, so consider converting 2 into the letter P.
When X=0, there are two situations:
When X is not equal to 0, there are also the following two situations:
P is not equal to 0, and it is subdivided into the following two situations:
The following figure shows the analysis processes in detail:
Define a one-dimensional number dp. The i-th element dp[i] represents the decoded number from the first character to the i-th character.
If the input is not null and the first character is not 0, dp[i]=1. Otherwise, 0 is returned directly.
According to the preceding analysis chart, it is summarized below:
The last item of the dp array is the solution to the problem.
var numDecodings = function (s) {
// dp[i] indicates all possible characters of the i-th character
const list = s.split('');
if (list.length === 0 || list[0] === '0') {
return 0;
}
const dp = [];
dp[0] = 1;
for (let i = 1; i < list.length; i++) {
const curChar = list[i];
if (curChar === '0') {
if (['1', '2'].includes(list[i - 1])) {
dp[i] = i >= 2 ? dp[i - 2] : 1;
} else {
return 0;
}
} else {
if (list[i - 1] === '0') {
dp[i] = dp[i - 1];
} else {
const number = Number(list[i - 1]) * 10 + Number(list[i]);
if (number <= 26) {
dp[i] = i >= 2 ? dp[i - 1] + dp[i - 2] : 2;
} else {
dp[i] = dp[i - 1];
}
}
}
}
console.log(dp);
return dp.pop();
}
Dynamic programming problems still require some experience. It is similar to the process of modeling and requires experience through continuous practice. In summary:
Points 1 and 2 are used to determine whether dynamic planning can be used to solve practical problems encountered quickly. Point 3 is a template to keep in mind to solve dynamic programming problems.
Pipcook 2.0 Implements the Training and Deployment of the Machine Learning Model in 20 Seconds!
41 posts | 1 followers
FollowAlibaba Clouder - December 26, 2016
JDP - December 17, 2021
Alibaba Clouder - March 22, 2018
Alibaba Clouder - April 3, 2018
Key - February 20, 2020
Apache Flink Community China - March 26, 2021
41 posts | 1 followers
FollowDynamic Content Delivery Network (DCDN) is a high-performance, reliable, and secure whole site acceleration solution.
Learn MoreAccelerate static and dynamic web content in a fast, reliable, and safe way using Secure DCDN (Dynamic Route for CDN)
Learn MoreMore Posts by Alibaba F(x) Team