1,Triangle
1 int mininumTotal(vector<vector<int>>& triangle) { 2 for (int i = triangle.size() - 2; i >= 0; --i) { 3 for (int j = 0; j < i + 1; ++j) { 4 // 从下往上依次保存当前路径的最小值,上层只会用到下层的最小值 5 triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]); 6 } 7 } 8 return triangle[0][0]; 9 }
triangle
2,Maximum SubArray
1 /* 2 * 状态转移方程为:f[j] = max{ f[j-1] + S[j],S[j] },其中 1 <= j <= n 3 * target = max{ f[j] },其中 1 <= j <= n 4 */ 5 int maxArray(vector<int>& nums) { 6 int n = nums.size(); 7 vector<int> dp(n + 1); 8 dp[0] = 0; 9 for (int i = 0; i < n; ++i) { 10 dp[i + 1] = max(dp[i] + nums[i], nums[i]); 11 } 12 return *max_element(dp.begin(), dp.end());
maxArray
3,
原文地址:https://www.cnblogs.com/zpcoding/p/10746370.html
时间: 2024-11-05 21:36:45