forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path64. Minimum Path Sum.go
64 lines (59 loc) · 1.13 KB
/
64. Minimum Path Sum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package leetcode
// 解法一 原地 DP,无辅助空间
func minPathSum(grid [][]int) int {
m, n := len(grid), len(grid[0])
for i := 1; i < m; i++ {
grid[i][0] += grid[i-1][0]
}
for j := 1; j < n; j++ {
grid[0][j] += grid[0][j-1]
}
for i := 1; i < m; i++ {
for j := 1; j < n; j++ {
grid[i][j] += min(grid[i-1][j], grid[i][j-1])
}
}
return grid[m-1][n-1]
}
// 解法二 最原始的方法,辅助空间 O(n^2)
func minPathSum1(grid [][]int) int {
if len(grid) == 0 {
return 0
}
m, n := len(grid), len(grid[0])
if m == 0 || n == 0 {
return 0
}
dp := make([][]int, m)
for i := 0; i < m; i++ {
dp[i] = make([]int, n)
}
// initFirstCol
for i := 0; i < len(dp); i++ {
if i == 0 {
dp[i][0] = grid[i][0]
} else {
dp[i][0] = grid[i][0] + dp[i-1][0]
}
}
// initFirstRow
for i := 0; i < len(dp[0]); i++ {
if i == 0 {
dp[0][i] = grid[0][i]
} else {
dp[0][i] = grid[0][i] + dp[0][i-1]
}
}
for i := 1; i < m; i++ {
for j := 1; j < n; j++ {
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]
}
}
return dp[m-1][n-1]
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}