public int minPathSum(final int[][] grid) {
if (grid.length == 1 && grid[0].length == 1) {
return grid[0][0];
}
final int[][] moves = {new int[]{1, 0}, new int[]{0, 1}};
final Queue<int[]> positions = new ArrayDeque<>();
final Queue<Integer> sums = new ArrayDeque<>();
positions.add(new int[]{0, 0});
sums.add(grid[0][0]);
int minSum = Integer.MAX_VALUE;
while (!positions.isEmpty()) {
final int[] point = positions.poll();
final int sum = sums.poll();
for (final int[] move : moves) {
final int x = point[0] + move[0];
final int y = point[1] + move[1];
if (x == grid.length - 1 && y == grid[0].length - 1) {
minSum = Math.min(minSum, sum);
} else if (x > -1 && y > -1 && x < grid.length && y < grid[0].length) {
positions.add(new int[]{x, y});
sums.add(sum + grid[x][y]);
}
}
}
return minSum + grid[grid.length - 1][grid[0].length - 1];
}
solution[0][0] = M[0][0]
// only one choice along
// the top horizontal and
// left vertical
solution[0][x] =
M[0][x] + solution[0][x - 1]
solution[y][0] =
M[y][0] + solution[y - 1][0]
// two choices otherwise:
// the best of option 1 or 2
solution[y][x] =
M[y][x] + min(
solution[y][x - 1],
solution[y - 1][x]
)
我们可以看到,我们可以创建一个适当的例程,例如使用 for 以“自下而上”的顺序 solution 访问矩阵的单元,
JavaScript 代码:
function show(M){
let str = '';
for (let row of M)
str += JSON.stringify(row) + '\n';
console.log(str);
}
function f(M){
console.log('Input:\n');
show(M);
let solution = new Array();
for (let i=0; i<M.length; i++)
solution.push(new Array(M[0].length).fill(Infinity));
solution[0][0] = M[0][0];
// only one choice along
// the top horizontal and
// left vertical
for (let x=1; x<M[0].length; x++)
solution[0][x] =
M[0][x] + solution[0][x - 1];
for (let y=1; y<M.length; y++)
solution[y][0] =
M[y][0] + solution[y - 1][0];
console.log('Solution borders:\n');
show(solution);
// two choices otherwise:
// the best of option 1 or 2
for (let y=1; y<M.length; y++)
for (let x=1; x<M[0].length; x++)
solution[y][x] =
M[y][x] + Math.min(
solution[y][x - 1],
solution[y - 1][x]
);
console.log('Full solution:\n');
show(solution);
return solution[M.length-1][M[0].length-1];
}
let arr = [];
arr[0] = [0, 7, -7];
arr[1] = [6, 7, -8];
arr[2] = [1, 2, 0];
console.log(f(arr));