首页 > 代码库 > LeetCode "Triangle"

LeetCode "Triangle"

This is the same as SPOJ #453. The best way to understand DP

1A code:

class Solution {public:    int minimumTotal(vector<vector<int> > &triangle) {                for(size_t i = 1; i < triangle.size(); i ++)        {            vector<int> &last = triangle[i-1];            vector<int> &curr = triangle[i];                        curr[0] = last[0] + curr[0];            for(size_t j = 1; j < curr.size() - 1; j ++)            {                int picked = last[j-1] < last[j] ? last[j-1] : last[j];                curr[j] += picked;            }            curr[curr.size() - 1] += last[curr.size() - 2];        }        //    Get min        vector<int> &last = triangle[triangle.size()-1];        return *std::min_element(last.begin(), last.end());    }};