Skip to content

Commit

Permalink
Fix the code in min_path_sum
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Oct 13, 2023
1 parent 3df5c36 commit a8c624f
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 48 deletions.
8 changes: 4 additions & 4 deletions codes/c/chapter_dynamic_programming/min_path_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ int minPathSumDFS(int gridCols, int grid[][gridCols], int i, int j) {
return INT_MAX;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
int left = minPathSumDFS(gridCols, grid, i - 1, j);
int up = minPathSumDFS(gridCols, grid, i, j - 1);
int up = minPathSumDFS(gridCols, grid, i - 1, j);
int left = minPathSumDFS(gridCols, grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
}
Expand All @@ -43,8 +43,8 @@ int minPathSumDFSMem(int gridCols, int grid[][gridCols], int mem[][gridCols], in
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
int left = minPathSumDFSMem(gridCols, grid, mem, i - 1, j);
int up = minPathSumDFSMem(gridCols, grid, mem, i, j - 1);
int up = minPathSumDFSMem(gridCols, grid, mem, i - 1, j);
int left = minPathSumDFSMem(gridCols, grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/cpp/chapter_dynamic_programming/min_path_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ int minPathSumDFS(vector<vector<int>> &grid, int i, int j) {
return INT_MAX;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
int left = minPathSumDFS(grid, i - 1, j);
int up = minPathSumDFS(grid, i, j - 1);
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
}
Expand All @@ -38,8 +38,8 @@ int minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i,
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
int left = minPathSumDFSMem(grid, mem, i - 1, j);
int up = minPathSumDFSMem(grid, mem, i, j - 1);
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/csharp/chapter_dynamic_programming/min_path_sum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public int MinPathSumDFS(int[][] grid, int i, int j) {
return int.MaxValue;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
int left = MinPathSumDFS(grid, i - 1, j);
int up = MinPathSumDFS(grid, i, j - 1);
int up = MinPathSumDFS(grid, i - 1, j);
int left = MinPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return Math.Min(left, up) + grid[i][j];
}
Expand All @@ -39,8 +39,8 @@ public int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
int left = MinPathSumDFSMem(grid, mem, i - 1, j);
int up = MinPathSumDFSMem(grid, mem, i, j - 1);
int up = MinPathSumDFSMem(grid, mem, i - 1, j);
int left = MinPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = Math.Min(left, up) + grid[i][j];
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/dart/chapter_dynamic_programming/min_path_sum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ int minPathSumDFS(List<List<int>> grid, int i, int j) {
return BigInt.from(2).pow(31).toInt();
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
int left = minPathSumDFS(grid, i - 1, j);
int up = minPathSumDFS(grid, i, j - 1);
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return min(left, up) + grid[i][j];
}
Expand All @@ -40,8 +40,8 @@ int minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
int left = minPathSumDFSMem(grid, mem, i - 1, j);
int up = minPathSumDFSMem(grid, mem, i, j - 1);
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = min(left, up) + grid[i][j];
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/go/chapter_dynamic_programming/min_path_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func minPathSumDFS(grid [][]int, i, j int) int {
return math.MaxInt
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
left := minPathSumDFS(grid, i-1, j)
up := minPathSumDFS(grid, i, j-1)
up := minPathSumDFS(grid, i-1, j)
left := minPathSumDFS(grid, i, j-1)
// 返回从左上角到 (i, j) 的最小路径代价
return int(math.Min(float64(left), float64(up))) + grid[i][j]
}
Expand All @@ -38,8 +38,8 @@ func minPathSumDFSMem(grid, mem [][]int, i, j int) int {
return mem[i][j]
}
// 左边和上边单元格的最小路径代价
left := minPathSumDFSMem(grid, mem, i-1, j)
up := minPathSumDFSMem(grid, mem, i, j-1)
up := minPathSumDFSMem(grid, mem, i-1, j)
left := minPathSumDFSMem(grid, mem, i, j-1)
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]
return mem[i][j]
Expand Down
8 changes: 4 additions & 4 deletions codes/java/chapter_dynamic_programming/min_path_sum.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ static int minPathSumDFS(int[][] grid, int i, int j) {
return Integer.MAX_VALUE;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
int left = minPathSumDFS(grid, i - 1, j);
int up = minPathSumDFS(grid, i, j - 1);
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return Math.min(left, up) + grid[i][j];
}
Expand All @@ -41,8 +41,8 @@ static int minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
int left = minPathSumDFSMem(grid, mem, i - 1, j);
int up = minPathSumDFSMem(grid, mem, i, j - 1);
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = Math.min(left, up) + grid[i][j];
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/javascript/chapter_dynamic_programming/min_path_sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function minPathSumDFS(grid, i, j) {
return Infinity;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
const left = minPathSumDFS(grid, i - 1, j);
const up = minPathSumDFS(grid, i, j - 1);
const up = minPathSumDFS(grid, i - 1, j);
const left = minPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return Math.min(left, up) + grid[i][j];
}
Expand All @@ -36,8 +36,8 @@ function minPathSumDFSMem(grid, mem, i, j) {
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
const left = minPathSumDFSMem(grid, mem, i - 1, j);
const up = minPathSumDFSMem(grid, mem, i, j - 1);
const up = minPathSumDFSMem(grid, mem, i - 1, j);
const left = minPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = Math.min(left, up) + grid[i][j];
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/python/chapter_dynamic_programming/min_path_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:
if i < 0 or j < 0:
return inf
# 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
left = min_path_sum_dfs(grid, i - 1, j)
up = min_path_sum_dfs(grid, i, j - 1)
up = min_path_sum_dfs(grid, i - 1, j)
left = min_path_sum_dfs(grid, i, j - 1)
# 返回从左上角到 (i, j) 的最小路径代价
return min(left, up) + grid[i][j]

Expand All @@ -36,8 +36,8 @@ def min_path_sum_dfs_mem(
if mem[i][j] != -1:
return mem[i][j]
# 左边和上边单元格的最小路径代价
left = min_path_sum_dfs_mem(grid, mem, i - 1, j)
up = min_path_sum_dfs_mem(grid, mem, i, j - 1)
up = min_path_sum_dfs_mem(grid, mem, i - 1, j)
left = min_path_sum_dfs_mem(grid, mem, i, j - 1)
# 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = min(left, up) + grid[i][j]
return mem[i][j]
Expand Down
8 changes: 4 additions & 4 deletions codes/rust/chapter_dynamic_programming/min_path_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {
return i32::MAX;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
let left = min_path_sum_dfs(grid, i - 1, j);
let up = min_path_sum_dfs(grid, i, j - 1);
let up = min_path_sum_dfs(grid, i - 1, j);
let left = min_path_sum_dfs(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
std::cmp::min(left, up) + grid[i as usize][j as usize]
}
Expand All @@ -36,8 +36,8 @@ fn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j
return mem[i as usize][j as usize];
}
// 左边和上边单元格的最小路径代价
let left = min_path_sum_dfs_mem(grid, mem, i - 1, j);
let up = min_path_sum_dfs_mem(grid, mem, i, j - 1);
let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);
let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];
mem[i as usize][j as usize]
Expand Down
8 changes: 4 additions & 4 deletions codes/swift/chapter_dynamic_programming/min_path_sum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {
return .max
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
let left = minPathSumDFS(grid: grid, i: i - 1, j: j)
let up = minPathSumDFS(grid: grid, i: i, j: j - 1)
let up = minPathSumDFS(grid: grid, i: i - 1, j: j)
let left = minPathSumDFS(grid: grid, i: i, j: j - 1)
// 返回从左上角到 (i, j) 的最小路径代价
return min(left, up) + grid[i][j]
}
Expand All @@ -36,8 +36,8 @@ func minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int
return mem[i][j]
}
// 左边和上边单元格的最小路径代价
let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)
let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)
let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)
let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = min(left, up) + grid[i][j]
return mem[i][j]
Expand Down
8 changes: 4 additions & 4 deletions codes/typescript/chapter_dynamic_programming/min_path_sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function minPathSumDFS(
return Infinity;
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
const left = minPathSumDFS(grid, i - 1, j);
const up = minPathSumDFS(grid, i, j - 1);
const up = minPathSumDFS(grid, i - 1, j);
const left = minPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return Math.min(left, up) + grid[i][j];
}
Expand All @@ -45,8 +45,8 @@ function minPathSumDFSMem(
return mem[i][j];
}
// 左边和上边单元格的最小路径代价
const left = minPathSumDFSMem(grid, mem, i - 1, j);
const up = minPathSumDFSMem(grid, mem, i, j - 1);
const up = minPathSumDFSMem(grid, mem, i - 1, j);
const left = minPathSumDFSMem(grid, mem, i, j - 1);
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[i][j] = Math.min(left, up) + grid[i][j];
return mem[i][j];
Expand Down
8 changes: 4 additions & 4 deletions codes/zig/chapter_dynamic_programming/min_path_sum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {
return std.math.maxInt(i32);
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
var left = minPathSumDFS(grid, i - 1, j);
var up = minPathSumDFS(grid, i, j - 1);
var up = minPathSumDFS(grid, i - 1, j);
var left = minPathSumDFS(grid, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
return @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
}
Expand All @@ -36,8 +36,8 @@ fn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {
return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
}
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
var left = minPathSumDFSMem(grid, mem, i - 1, j);
var up = minPathSumDFSMem(grid, mem, i, j - 1);
var up = minPathSumDFSMem(grid, mem, i - 1, j);
var left = minPathSumDFSMem(grid, mem, i, j - 1);
// 返回从左上角到 (i, j) 的最小路径代价
// 记录并返回左上角到 (i, j) 的最小路径代价
mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];
Expand Down

0 comments on commit a8c624f

Please sign in to comment.