This paper starts with a common algorithm of BAT interview, introduces the basic concept of dynamic programming, and introduces its thinking process.
One, the most common algorithm interview question on BAT -- up the steps.
There is a staircase with a total of N steps. It can only go up, only 1 or 2 steps at a time. How many ways are there altogether?
1, permutation and combination;
Enumerate the number of 2 and enumerate the location of 2.
The calculation is complex and easy to omit.
2, dynamic programming;
Dp[n] stands for N steps.
Clear thinking and simple code.
Two. Basic concepts of dynamic programming
1, dynamic programming;
Dynamic Programming is a way to solve optimization problems.
2, the optimal substructure property;
The optimal solution of the problem can be decomposed into several sub problems, and the solution of the sub problem is also optimal.
Taking the above steps as an example, the method of going up to the I level can be decomposed into the sum of the I-1 and I-2 layers, and the I-1 and I-2 layers are the most common ones.
3, no aftereffect;
Decisions at this stage will not affect future decisions.
Taking the above steps as an example, the method of walking up to the I-2 level will not change due to the increase of the I-1 level.
Three. Dynamic planning thinking process
The thinking process of dynamic programming can be summarized as follows: Big business is small and trivial matters happen.
Big trouble is small:
A larger problem is to divide complex problems into small ones by finding overlaps with sub problems, also known as state transitions.
Small problems are usually solved by initialization and direct calculation.
1, decomposing large problems into sub problems.
2. Determine the state representation.
3. Determine the status transition.
4. Consider the initial state and boundary conditions.
Four, another classic example -- the number tower.
As is shown in the figures, the number tower is required to go from the top to the bottom. If each step can only go to the adjacent nodes, what is the maximum sum of the number of nodes passing through it?
Topic linking point Here
1, big business is small. To reach Tier I, we must first arrive at level I-1. Moreover, the j node in Tier I can only be reached by the J and J-1 nodes of the I-1 layer.
We use dp[i][j] to represent the largest sum of numbers in the j position at level I.
Then there is dp[i][j]=max (dp[i-1][j], dp[i-1][j-1]) + a[i][j];
2, small incident. The first node of the first level, the initial value is Dp=a 。 (a[x][y] denotes level x, value of Y).
Five. Deformation of several tower examples - collecting apples
There are N*M lattices on the plane, and a number of apples are placed in each grid.
You start from the top left corner of the grid, each step can only go down or to the right. Every time you go to a lattice, you collect the apples in the lattice.
How many apples can you collect in this way?
1, we can only walk to the right or go downwards. When we arrive at the lattice of column J of line I, we can arrive at row J of line I-1 or J-1 row of I row. We use dp[i][j] to indicate the maximum number of apples to j row of I row.
Dp[i][j]=max (dp[i-1][j], dp[i][j-1]) + a[i][j];
2, first rows first columns, the initial value is Dp=a The precaution is the handling of boundary conditions.
Six, dynamic programming classic -- 01 knapsack problem
Given a n item and a backpack with a capacity of M, each item consumes a certain volume c[i] of the knapsack, and brings a certain value v[i]. It asks how to select items loaded into the backpack to maximize the value of the items in the backpack.
Putting n items in the backpack can be broken down into the problem of putting the former I items into a m capacity backpack.
If we consider only the choice of item I, the problem can be divided into two parts:
1, if you don't put a I item, the problem will be turned into " The front I-1 items are put in a backpack with a capacity of v. ";
2, if you place a I item, the problem will be turned into " The former I-1 items are put into the backpack with a capacity of m-c[i]. ";
We use f[i][j] to represent the top I items and put the maximum value of the knapsack with a capacity of J. The above two cases can be expressed as
F[i][j] = max (f[i-1][j], f[i-1][j-c[i]]+v[i]);
Initialization condition memset (DP, 0, sizeof (DP)); and dp[c]=v.
Finally, the maximum value can be obtained by traversing f[n][1~m].
If you can't fully understand 01 knapsack, you need to understand it carefully. Optimal substructure, state representation and state transition 。
The algorithm can expand the direction of thinking and improve the ability of thinking. Learn " "Step up", "tower", "01 knapsack" These three topics can solve the dynamic programming part of the algorithm interview.
Author: Luo Ying loyinglin