A very simple DP, we can see that the space limit is O (N). Do not habitually think that it is to save the minimum value of each row, it is easy to think of the sum of the numbers in a row when they are the end points of the path. When the last row is reached, it is the shortest path and at the bottom position as the end point from the top to the bottom. You can find the smallest one.
You should pay attention to this problem when implementing it. Because we need to use the data of the previous row During computation, we should calculate it from the back to avoid data overwriting. This technique is used a lot, I believe everyone is familiar with it.
The ac code is as follows:
Class Solution {public: int minimumTotal (vector
> & Triangle) {int lines = triangle. size (); if (lines = 0) return 0; vector
Res (lines, 0); for (int I = 0; I
0) res [I] = res [I-1] + triangle [I] [I]; for (int j = I-1; j> 0; j --) {res [j] = min (res [J-1], res [j]) + triangle [I] [j];} res [0] = res [0] + triangle [I] [0];} int mmin = res [0]; for (int I = 1; I