[Leetcode] 120 Triangle
After acm is done, this question is too basic ....
There is nothing to say. The simplest dynamic planning is to find the state transfer equation. The bottom-up approach is used (in this way, only dp [0] [0] is the answer). The results of each node in the current layer are calculated based on the accumulated subgrade of the next row, take either the left or the right, and take the smallest of the two.
State transition equation: triangle [I] [j] + = min (triangle [I + 1] [j], triangle [I + 1] [j + 1])
Class Solution {public: int minimumTotal (vector
> & Triangle) {for (int I = triangle. size ()-2; I> = 0; -- I) for (int j = 0; j <= I; ++ j) {triangle [I] [j] + = min (triangle [I + 1] [j + 1], triangle [I + 1] [j]);} return triangle [0] [0] ;}};
Similar questions include the hdu 2084 data Tower, which is consistent with the solution in this question.
More advanced: hdu 1176 free pie
Train of Thought: we can regard all the time periods and pies as a matrix, where time is the number of rows, and the number of columns is the number triangle. We can find a path from the bottom layer, make the sum on the path largest. The state transition equation is dp [I] [j] = max (dp [I + 1] [J-1], dp [I + 1] [j], dp [I + 1] [J-1]) + pie [I] [j]. Pie [I] [j] is the number of pies dropped at the position j at the time I.
The specific code will not be posted here. You should try your best to implement it.