2014.1.9 22:56
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is 11
(i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
Solution:
First of all, you can't solve this problem with greedy strategy, i.e. choosing the smaller one on every branch you go down. It's dynamic programming.
Let f[i][j] be the minimum path sum you can get with a path ending at position (i, j). We have the following:
1. f[0][0] = a[0][0]; // You surely know a is an array, right?
2. f[i][i] = f[i - 1][i - 1] + a[i][i]; // i > 0
3. f[i][j] = min(f[i - 1][j - 1], f[i - 1][j]) + a[i][j]; // j > 0 && j < i
Time complexity is O(n^2). Space complexity is O(n). The recurrence relation indicates that the current row is only related to the previous row, thus the space needed is O(n), instead of O(n^2).
Accepted code:
1 // 4CE, 2WA, 1AC, calm down~ 2 class Solution { 3 public: 4 int minimumTotal(vector<vector<int> > &triangle) { 5 // IMPORTANT: Please reset any member data you declared, as 6 // the same Solution instance will be reused for each test case. 7 int n; 8 vector<vector<int>> vv; 9 int ff; 10 11 n = triangle.size(); 12 if(n == 0){ 13 return 0; 14 } 15 16 int i, j; 17 18 for(i = 0; i < 2; ++i){ 19 vv.push_back(vector<int>()); 20 for(j = 0; j < n; ++j){ 21 vv[i].push_back(0); 22 } 23 } 24 25 ff = 0; 26 vv[ff][0] = triangle[0][0]; 27 for(i = 1; i < n; ++i){ 28 vv[!ff][0] = vv[ff][0] + triangle[i][0]; 29 for(j = 1; j < i; ++j){ 30 // 2WA here, wrong coding 31 vv[!ff][j] = mymin(vv[ff][j - 1], vv[ff][j]) + triangle[i][j]; 32 } 33 // 4CE here, foolish coding!!! 34 vv[!ff][i] = vv[ff][i - 1] + triangle[i][i]; 35 ff = !ff; 36 } 37 38 int res = vv[ff][0]; 39 for(i = 1; i < n; ++i){ 40 res = mymin(res, vv[ff][i]); 41 } 42 43 vv[0].clear(); 44 vv[1].clear(); 45 vv.clear(); 46 47 return res; 48 } 49 private: 50 const int mymin(const int &x, const int &y) { 51 return (x < y ? x : y); 52 } 53 };