|
| 1 | +[](https://github.com/javadev/LeetCode-in-All) |
| 2 | +[](https://github.com/javadev/LeetCode-in-All/fork) |
| 3 | + |
| 4 | +## 64\. Minimum Path Sum |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +**Note:** You can only move either down or right at any point in time. |
| 11 | + |
| 12 | +**Example 1:** |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]] |
| 17 | + |
| 18 | +**Output:** 7 |
| 19 | + |
| 20 | +**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. |
| 21 | + |
| 22 | +**Example 2:** |
| 23 | + |
| 24 | +**Input:** grid = \[\[1,2,3],[4,5,6]] |
| 25 | + |
| 26 | +**Output:** 12 |
| 27 | + |
| 28 | +**Constraints:** |
| 29 | + |
| 30 | +* `m == grid.length` |
| 31 | +* `n == grid[i].length` |
| 32 | +* `1 <= m, n <= 200` |
| 33 | +* `0 <= grid[i][j] <= 100` |
| 34 | + |
| 35 | +To solve the "Minimum Path Sum" problem in Swift with the Solution class, follow these steps: |
| 36 | + |
| 37 | +1. Define a method `minPathSum` in the `Solution` class that takes a 2D grid of non-negative numbers as input and returns the minimum sum of all numbers along the path from the top-left corner to the bottom-right corner of the grid. |
| 38 | +2. Initialize a 2D array `dp` of size `m x n`, where `dp[i][j]` represents the minimum sum of the path from the top-left corner to position `(i, j)` in the grid. |
| 39 | +3. Initialize `dp[0][0]` to the value of the top-left cell in the grid. |
| 40 | +4. Initialize the first row and first column of `dp` based on the grid values and the previous cells in the same row or column. |
| 41 | +5. Iterate over each position `(i, j)` in the grid, starting from the second row and second column: |
| 42 | + - Update `dp[i][j]` by adding the current grid value at `(i, j)` to the minimum of the values of the previous cells `(i-1, j)` and `(i, j-1)` in `dp`. |
| 43 | +6. Return `dp[m-1][n-1]`, which represents the minimum path sum from the top-left corner to the bottom-right corner of the grid. |
| 44 | + |
| 45 | +Here's the implementation of the `minPathSum` method in Swift: |
| 46 | + |
| 47 | +```swift |
| 48 | +class Solution { |
| 49 | + func minPathSum(_ grid: [[Int]]) -> Int { |
| 50 | + var matrix: [[Int]] = grid |
| 51 | + let n = grid.count - 1 |
| 52 | + let m = grid[0].count - 1 |
| 53 | + |
| 54 | + for i in 0...n { |
| 55 | + for j in 0...m { |
| 56 | + var step = matrix[i][j] |
| 57 | + |
| 58 | + if i > 0 && j > 0 { |
| 59 | + step += min(matrix[i - 1][j], matrix[i][j - 1]) |
| 60 | + } else if i > 0 && j == 0 { |
| 61 | + step += matrix[i - 1][j] |
| 62 | + } else if j > 0 && i == 0 { |
| 63 | + step += matrix[i][j - 1] |
| 64 | + } |
| 65 | + |
| 66 | + matrix[i][j] = step |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return matrix[n][m] |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +This implementation efficiently calculates the minimum path sum using dynamic programming, with a time complexity of O(m * n) and a space complexity of O(m * n). |
0 commit comments