题解 | #最小体重积#
最小体重积
https://www.nowcoder.com/practice/0980f806727e48f3b0253243416038c0
知识点:
动态规划
分析:
动态规划,相乘;
创建一个dp数组,然后以某个路径(如:dp[i][j])为结尾,找这个路径最小的值乘以cows中的值。
完整代码:ACcode c++
long long f[110][110]; long long minPathProduct(vector<vector<int> >& cows) { // write code here int m = cows.size(), n = cows[0].size(); for(int i = 0;i<=m;++i) for(int j = 0;j<=n;j++) f[i][j] = LONG_MAX; f[0][1] = 1; for (int i = 1; i <= m; ++i) for (int j = 1; j <= n; ++j) f[i][j] = min(f[i-1][j], f[i][j-1]) * cows[i-1][j-1]; return f[m][n]; }
优化后代码:
long long f[110]; long long minPathProduct(vector<vector<int> >& cows) { // write code here int m = cows.size(), n = cows[0].size(); for(int i = 0;i<=n;++i) f[i] = LONG_MAX; f[1] = 1; for (int i = 1; i <= m; ++i) for (int j = 1; j <= n; ++j) f[j] = min(f[j], f[j-1]) * cows[i-1][j-1]; return f[n]; }