//L120 Triangle
//!!!IMPORTANT:erase an DP array to save space, if we only count numbers rather than to know the path
//edge case: NULL
//thoughts:DP, use a arr[n] to store the smallest element in every line, each element in arr[] represents the smaller ele in the bottom line corresponding to this ele
//construct from the bottom line
//a little detail:
//we compare triangle[i][j]+arr[j] with triangle[i][j+1]+arr[j+1], the smaller is written to arr[j](i from 1 to n,j from 0 to i-2)
//when we reach the arr[i-1], then we finished this line
int minimumTotal(vector<vector<int> > &triangle)
{
if (triangle.empty())
return 0;
int n = triangle.size();
int arr[n]; //a new feature supported by C99
memset(arr, 0,n * sizeof(int)); //clear the temp array
int i, j;
for (i = n - 1; i >= 0; i--)
{
for (j = 0; j < i; j++)
{
arr[j] = std::min(triangle[i][j] + arr[j],
triangle[i][j + 1] + arr[j + 1]);
}
}
return triangle[0][0] + arr[0]; //return the top elemnt plus the smaller of its two "childeren"
}