给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
示例:
输入:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
输出: 7
解释: 因为路径 1→3→1→1→1 的总和最小。
var minPathSum = function (grid) {
let rowLength = grid[0].length;
let colLength = grid.length;
let i = 0, j = 0;
var now = grid[i][j];
var right = grid[i][j + 1];
var bottom = grid[i + 1][i];
for (i; i <= colLength; i++) {
i = now + right >= now + bottom ? i + 1 : i;
for (j; j <= rowLength; j++) {
j = now + right >= now + bottom ? j : j + 1;
now = now + right >= now + bottom ? now + bottom : now + right;
}
now = now + right >= now + bottom ? now + bottom : now + right;
}
return now
};
console.log(minPathSum([[1, 3, 1], [1, 5, 1], [4, 2, 1]]))