题解 | #三角形最小路径和#
三角形最小路径和
https://www.nowcoder.com/practice/c9d44b73dc7c4dbfa4272224b1f9b42c
#include <climits> #include <cstdio> #include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param triangle int整型vector<vector<>> * @return int整型 */ int minTrace(vector<vector<int> >& triangle) { // write code here int ans = INT_MAX; int n = triangle.size(); vector<int>dp(n,INT_MIN); dp[0] = triangle[0][0]; for (int i = 1; i < n; i++) { for (int j = i; j >= 0; j--) { if(j>0 && j<i) { dp[j]=min(dp[j],dp[j-1])+triangle[i][j]; }else if(j==0) { dp[j]=dp[j]+triangle[i][j]; }else { dp[j]=dp[j-1]+triangle[i][j]; } } } for(int i=0;i<n;i++) { ans=min(ans,dp[i]); } return ans; } };