题目描述
众所周知,牛妹有很多很多粉丝,粉丝送了很多很多礼物给牛妹,牛妹的礼物摆满了地板。
地板是N\times MN×M的格子,每个格子有且只有一个礼物,牛妹已知每个礼物的体积。
地板的坐标是左上角(1,1) 右下角(N, M)。
牛妹只想要从屋子左上角走到右下角,每次走一步,每步只能向下走一步或者向右走一步或者向右下走一步
每次走过一个格子,拿起(并且必须拿上)这个格子上的礼物。
牛妹想知道,她能走到最后拿起的所有礼物体积最小和是多少?
import java.util.*;
public class Solution {
/**
*
* @param presentVolumn int整型二维数组 N*M的矩阵,每个元素是这个地板砖上的礼物体积
* @return int整型
*/
public int selectPresent (int[][] presentVolumn) {
// write code here
for (int i = 0; i < presentVolumn.length; i++) {
for (int j = 0; j < presentVolumn[0].length; j++) {
if (i == 0) {
if (j == 0)
continue;
else
presentVolumn[i][j] += presentVolumn[i][j - 1];
} else if (j == 0) {
presentVolumn[i][j] += presentVolumn[i - 1][j];
} else {
presentVolumn[i][j] += Math.min(Math.min(presentVolumn[i][j - 1], presentVolumn[i - 1][j]), presentVolumn[i - 1][j - 1]);
}
}
}
return presentVolumn[presentVolumn.length - 1][presentVolumn[0].length - 1];
}
}