简单的不能再简单的dp了
但有时很经典啊。。。作为面向大众普通同学的题库来说,也是要会的啊。。。
要是非要再白痴的做法难不成是把所有方案遍历一遍?
真的没有更简单的dp了QAQ 让我写简单做法真的找不到了QAQ 所有方案遍历一遍更麻烦啊 QAQ
因为是简单题,所以不想在空间 时间复杂度上做文章
class Solution {
public:
/**
*
* @param presentVolumn int整型vector<vector<>> N*M的矩阵,每个元素是这个地板砖上的礼物体积
* @return int整型
*/
int min (int a, int b, int c) {
if (a < b)
return a < c ? a: c;
else
return b < c ? b: c;
}
int selectPresent(vector<vector<int> >& presentVolumn) {
// write code here
int n = presentVolumn.size();
if (n==0) return 0;
int m = presentVolumn[0].size();
if (m==0) return 0;
for (int j = 1; j < m; j ++)
presentVolumn[0][j] += presentVolumn[0][j-1];
for (int i = 1; i < n; i ++) {
presentVolumn[i][0] += presentVolumn[i-1][0];
for (int j = 1; j < m; j ++) {
presentVolumn[i][j] += min(presentVolumn[i-1][j],
presentVolumn[i-1][j-1],
presentVolumn[i][j-1]);
}
}
return presentVolumn[n-1][m-1];
}
};

京公网安备 11010502036488号