|
47 | 47 |
|
48 | 48 | <!-- 这里可写通用的实现逻辑 -->
|
49 | 49 |
|
| 50 | +动态规划。假设 `dp[i][j]` 表示到达网格 `(i,j)` 的最小数字和,先初始化 dp 第一列和第一行的所有值,然后利用递推公式:`dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]` 求得 dp。 |
| 51 | + |
| 52 | +最后返回 `dp[m - 1][n - 1]` 即可。 |
| 53 | + |
50 | 54 | <!-- tabs:start -->
|
51 | 55 |
|
52 | 56 | ### **Python3**
|
53 | 57 |
|
54 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 59 |
|
56 | 60 | ```python
|
57 |
| - |
| 61 | +class Solution: |
| 62 | + def minPathSum(self, grid: List[List[int]]) -> int: |
| 63 | + m, n = len(grid), len(grid[0]) |
| 64 | + dp = [[grid[0][0]] * n for _ in range(m)] |
| 65 | + for i in range(1, m): |
| 66 | + dp[i][0] = dp[i - 1][0] + grid[i][0] |
| 67 | + for j in range(1, n): |
| 68 | + dp[0][j] = dp[0][j - 1] + grid[0][j] |
| 69 | + for i in range(1, m): |
| 70 | + for j in range(1, n): |
| 71 | + dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j] |
| 72 | + return dp[-1][-1] |
58 | 73 | ```
|
59 | 74 |
|
60 | 75 | ### **Java**
|
61 | 76 |
|
62 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 78 |
|
64 | 79 | ```java
|
| 80 | +class Solution { |
| 81 | + public int minPathSum(int[][] grid) { |
| 82 | + int m = grid.length, n = grid[0].length; |
| 83 | + int[][] dp = new int[m][n]; |
| 84 | + dp[0][0] = grid[0][0]; |
| 85 | + for (int i = 1; i < m; ++i) { |
| 86 | + dp[i][0] = dp[i - 1][0] + grid[i][0]; |
| 87 | + } |
| 88 | + for (int j = 1; j < n; ++j) { |
| 89 | + dp[0][j] = dp[0][j - 1] + grid[0][j]; |
| 90 | + } |
| 91 | + for (int i = 1; i < m; ++i) { |
| 92 | + for (int j = 1; j < n; ++j) { |
| 93 | + dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; |
| 94 | + } |
| 95 | + } |
| 96 | + return dp[m - 1][n - 1]; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### **TypeScript** |
| 102 | + |
| 103 | +```ts |
| 104 | +function minPathSum(grid: number[][]): number { |
| 105 | + let m = grid.length, n = grid[0].length; |
| 106 | + let dp = Array.from({ length: m}, v => new Array(n).fill(0)); |
| 107 | + dp[0][0] = grid[0][0]; |
| 108 | + for (let i = 1; i < m; ++i) { |
| 109 | + dp[i][0] = dp[i - 1][0] + grid[i][0]; |
| 110 | + } |
| 111 | + for (let j = 1; j < n; ++j) { |
| 112 | + dp[0][j] = dp[0][j - 1] + grid[0][j]; |
| 113 | + } |
| 114 | + // dp |
| 115 | + for (let i = 1; i < m; ++i) { |
| 116 | + for (let j = 1; j < n; ++j) { |
| 117 | + let cur = grid[i][j]; |
| 118 | + dp[i][j] = cur + Math.min(dp[i - 1][j], dp[i][j - 1]); |
| 119 | + } |
| 120 | + } |
| 121 | + return dp[m - 1][n - 1]; |
| 122 | +}; |
| 123 | +``` |
| 124 | + |
| 125 | +### **C++** |
| 126 | + |
| 127 | +```cpp |
| 128 | +class Solution { |
| 129 | +public: |
| 130 | + int minPathSum(vector<vector<int>> &grid) { |
| 131 | + int m = grid.size(), n = grid[0].size(); |
| 132 | + vector<vector<int>> dp(m, vector<int>(n, grid[0][0])); |
| 133 | + for (int i = 1; i < m; ++i) |
| 134 | + { |
| 135 | + dp[i][0] = dp[i - 1][0] + grid[i][0]; |
| 136 | + } |
| 137 | + for (int j = 1; j < n; ++j) |
| 138 | + { |
| 139 | + dp[0][j] = dp[0][j - 1] + grid[0][j]; |
| 140 | + } |
| 141 | + for (int i = 1; i < m; ++i) |
| 142 | + { |
| 143 | + for (int j = 1; j < n; ++j) |
| 144 | + { |
| 145 | + dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; |
| 146 | + } |
| 147 | + } |
| 148 | + return dp[m - 1][n - 1]; |
| 149 | + } |
| 150 | +}; |
| 151 | +``` |
| 152 | +
|
| 153 | +### **Go** |
| 154 | +
|
| 155 | +```go |
| 156 | +func minPathSum(grid [][]int) int { |
| 157 | + m, n := len(grid), len(grid[0]) |
| 158 | + dp := make([][]int, m) |
| 159 | + for i := 0; i < m; i++ { |
| 160 | + dp[i] = make([]int, n) |
| 161 | + } |
| 162 | + dp[0][0] = grid[0][0] |
| 163 | + for i := 1; i < m; i++ { |
| 164 | + dp[i][0] = dp[i-1][0] + grid[i][0] |
| 165 | + } |
| 166 | + for j := 1; j < n; j++ { |
| 167 | + dp[0][j] = dp[0][j-1] + grid[0][j] |
| 168 | + } |
| 169 | + for i := 1; i < m; i++ { |
| 170 | + for j := 1; j < n; j++ { |
| 171 | + dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] |
| 172 | + } |
| 173 | + } |
| 174 | + return dp[m-1][n-1] |
| 175 | +} |
| 176 | +
|
| 177 | +func min(a, b int) int { |
| 178 | + if a < b { |
| 179 | + return a |
| 180 | + } |
| 181 | + return b |
| 182 | +} |
| 183 | +``` |
65 | 184 |
|
| 185 | +### **C#** |
| 186 | + |
| 187 | +```cs |
| 188 | +public class Solution { |
| 189 | + public int MinPathSum(int[][] grid) { |
| 190 | + int m = grid.Length, n = grid[0].Length; |
| 191 | + int[,] dp = new int[m, n]; |
| 192 | + dp[0, 0] = grid[0][0]; |
| 193 | + for (int i = 1; i < m; ++i) |
| 194 | + { |
| 195 | + dp[i, 0] = dp[i - 1, 0] + grid[i][0]; |
| 196 | + } |
| 197 | + for (int j = 1; j < n; ++j) |
| 198 | + { |
| 199 | + dp[0, j] = dp[0, j - 1] + grid[0][j]; |
| 200 | + } |
| 201 | + for (int i = 1; i < m; ++i) |
| 202 | + { |
| 203 | + for (int j = 1; j < n; ++j) |
| 204 | + { |
| 205 | + dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + grid[i][j]; |
| 206 | + } |
| 207 | + } |
| 208 | + return dp[m- 1, n - 1]; |
| 209 | + } |
| 210 | +} |
66 | 211 | ```
|
67 | 212 |
|
68 | 213 | ### **...**
|
|
0 commit comments