首页 > 代码库 > 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()); }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。