码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Triangle"

时间:2014-07-18 21:19:47      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   for   

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());
    }
};

LeetCode "Triangle",布布扣,bubuko.com

LeetCode "Triangle"

标签:style   blog   http   color   io   for   

原文地址:http://www.cnblogs.com/tonix/p/3850104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!