mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 14:20:43 +08:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
This commit is contained in:
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_path_sum {
|
||||
/* 最小路径和:暴力搜索 */
|
||||
public int minPathSumDFS(int[][] grid, int i, int j) {
|
||||
public int MinPathSumDFS(int[][] grid, int i, int j) {
|
||||
// 若为左上角单元格,则终止搜索
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
@@ -18,14 +18,14 @@ public class min_path_sum {
|
||||
return int.MaxValue;
|
||||
}
|
||||
// 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价
|
||||
int left = minPathSumDFS(grid, i - 1, j);
|
||||
int up = minPathSumDFS(grid, i, j - 1);
|
||||
int left = MinPathSumDFS(grid, i - 1, j);
|
||||
int up = MinPathSumDFS(grid, i, j - 1);
|
||||
// 返回从左上角到 (i, j) 的最小路径代价
|
||||
return Math.Min(left, up) + grid[i][j];
|
||||
}
|
||||
|
||||
/* 最小路径和:记忆化搜索 */
|
||||
public int minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
|
||||
public int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
|
||||
// 若为左上角单元格,则终止搜索
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
@@ -39,15 +39,15 @@ public class min_path_sum {
|
||||
return mem[i][j];
|
||||
}
|
||||
// 左边和上边单元格的最小路径代价
|
||||
int left = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||
int up = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||
int left = MinPathSumDFSMem(grid, mem, i - 1, j);
|
||||
int up = MinPathSumDFSMem(grid, mem, i, j - 1);
|
||||
// 记录并返回左上角到 (i, j) 的最小路径代价
|
||||
mem[i][j] = Math.Min(left, up) + grid[i][j];
|
||||
return mem[i][j];
|
||||
}
|
||||
|
||||
/* 最小路径和:动态规划 */
|
||||
public int minPathSumDP(int[][] grid) {
|
||||
public int MinPathSumDP(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n, m];
|
||||
@@ -70,7 +70,7 @@ public class min_path_sum {
|
||||
}
|
||||
|
||||
/* 最小路径和:空间优化后的动态规划 */
|
||||
public int minPathSumDPComp(int[][] grid) {
|
||||
public int MinPathSumDPComp(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[m];
|
||||
@@ -104,7 +104,7 @@ public class min_path_sum {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
|
||||
// 暴力搜索
|
||||
int res = minPathSumDFS(grid, n - 1, m - 1);
|
||||
int res = MinPathSumDFS(grid, n - 1, m - 1);
|
||||
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
|
||||
|
||||
// 记忆化搜索
|
||||
@@ -113,15 +113,15 @@ public class min_path_sum {
|
||||
mem[i] = new int[m];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
|
||||
res = MinPathSumDFSMem(grid, mem, n - 1, m - 1);
|
||||
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
|
||||
|
||||
// 动态规划
|
||||
res = minPathSumDP(grid);
|
||||
res = MinPathSumDP(grid);
|
||||
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
|
||||
|
||||
// 空间优化后的动态规划
|
||||
res = minPathSumDPComp(grid);
|
||||
res = MinPathSumDPComp(grid);
|
||||
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user