-
Notifications
You must be signed in to change notification settings - Fork 0
/
64.minimum-path-sum.go
103 lines (91 loc) · 1.99 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* @lc app=leetcode id=64 lang=golang
*
* [64] Minimum Path Sum
*
* https://leetcode.com/problems/minimum-path-sum/description/
*
* algorithms
* Medium (54.60%)
* Likes: 3419
* Dislikes: 64
* Total Accepted: 446.9K
* Total Submissions: 816.3K
* Testcase Example: '[[1,3,1],[1,5,1],[4,2,1]]'
*
* Given a m x n grid filled with non-negative numbers, find a path from top
* left to bottom right which minimizes the sum of all numbers along its path.
*
* Note: You can only move either down or right at any point in time.
*
* Example:
*
*
* Input:
* [
* [1,3,1],
* [1,5,1],
* [4,2,1]
* ]
* Output: 7
* Explanation: Because the path 1→3→1→1→1 minimizes the sum.
*
*
*/
// @lc code=start
func minPathSum(grid [][]int) int {
return minPathSum2(grid)
}
func minPathSum2(grid [][]int) int {
if len(grid) == 0 || len(grid[0]) == 0 {
return 0
}
row, col := len(grid), len(grid[0])
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
if i == 0 && j > 0 {
grid[i][j] = grid[i][j] + grid[i][j-1]
} else if i > 0 && j == 0 {
grid[i][j] = grid[i][j] + grid[i-1][j]
} else if i > 0 && j > 0 {
if grid[i-1][j] < grid[i][j-1] {
grid[i][j] = grid[i-1][j] + grid[i][j]
} else {
grid[i][j] = grid[i][j-1] + grid[i][j]
}
}
}
}
return grid[row-1][col-1]
}
// dp[i][j] = min(dp[i-1][j], dp[i][j]) + grid[i][j]
func minPathSum1(grid [][]int) int {
if len(grid) == 0 || len(grid[0]) == 0 {
return 0
}
row, col := len(grid), len(grid[0])
sum := make([][]int, row)
for i := 0; i < row; i++ {
sum[i] = make([]int, col)
}
sum[0][0] = grid[0][0]
for i := 1; i < row; i++ {
sum[i][0] = sum[i-1][0] + grid[i][0]
}
for i := 1; i < col; i++ {
sum[0][i] = sum[0][i-1] + grid[0][i]
}
for i := 1; i < row; i++ {
for j := 1; j < col; j++ {
sum[i][j] = min(sum[i-1][j], sum[i][j-1]) + grid[i][j]
}
}
return sum[row-1][col-1]
}
func min(val1, val2 int) int {
if val1 < val2 {
return val1
}
return val2
}
// @lc code=end