Testing Minimum Path Sum Algorithm Implementations in hello-algo
This test suite evaluates implementations of the minimum path sum algorithm using different approaches in Go. It validates path finding solutions using brute force search, memoization, dynamic programming, and space-optimized dynamic programming methods.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
krahets/hello-algo
codes/go/chapter_dynamic_programming/min_path_sum_test.go
// File: min_path_sum_test.go
// Created Time: 2023-07-23
// Author: Reanon ([email protected])
package chapter_dynamic_programming
import (
"fmt"
"testing"
)
func TestMinPathSum(t *testing.T) {
grid := [][]int{
{1, 3, 1, 5},
{2, 2, 4, 2},
{5, 3, 2, 1},
{4, 3, 5, 2},
}
n, m := len(grid), len(grid[0])
// 暴力搜索
res := minPathSumDFS(grid, n-1, m-1)
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
// 记忆化搜索
mem := make([][]int, n)
for i := 0; i < n; i++ {
mem[i] = make([]int, m)
for j := 0; j < m; j++ {
mem[i][j] = -1
}
}
res = minPathSumDFSMem(grid, mem, n-1, m-1)
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
// 动态规划
res = minPathSumDP(grid)
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
// 空间优化后的动态规划
res = minPathSumDPComp(grid)
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
}