|
| 1 | +# Challenge Description and Solution |
| 2 | + |
| 3 | +## English Version |
| 4 | + |
| 5 | +### Challenge Description |
| 6 | +Given a matrix of integers, determine the minimum sum to reach from the top-left corner to the bottom-right corner, moving only right or down. Use dynamic programming to optimize the solution. |
| 7 | + |
| 8 | +### Code Explanation |
| 9 | +The solution uses a 2D dynamic programming table `dp` where `dp[i][j]` represents the minimum path sum to reach cell `(i, j)`. |
| 10 | + |
| 11 | +The first row and first column are initialized by accumulating sums from the start. For other cells, the minimum path sum is the cell's value plus the minimum of the sums from the top or left neighbor. |
| 12 | + |
| 13 | +### Relevant Code Snippet |
| 14 | + |
| 15 | +```javascript |
| 16 | +function minPathSum(grid) { |
| 17 | + if (!grid || grid.length === 0 || grid[0].length === 0) { |
| 18 | + return 0; |
| 19 | + } |
| 20 | + |
| 21 | + const m = grid.length; |
| 22 | + const n = grid[0].length; |
| 23 | + const dp = Array.from({ length: m }, () => Array(n).fill(0)); |
| 24 | + |
| 25 | + dp[0][0] = grid[0][0]; |
| 26 | + |
| 27 | + for (let i = 1; i < m; i++) { |
| 28 | + dp[i][0] = dp[i - 1][0] + grid[i][0]; |
| 29 | + } |
| 30 | + |
| 31 | + for (let j = 1; j < n; j++) { |
| 32 | + dp[0][j] = dp[0][j - 1] + grid[0][j]; |
| 33 | + } |
| 34 | + |
| 35 | + for (let i = 1; i < m; i++) { |
| 36 | + for (let j = 1; j < n; j++) { |
| 37 | + dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return dp[m - 1][n - 1]; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Example Usage |
| 46 | + |
| 47 | +```javascript |
| 48 | +import minPathSum from './minPathSum.js'; |
| 49 | + |
| 50 | +const grid = [ |
| 51 | + [1, 3, 1], |
| 52 | + [1, 5, 1], |
| 53 | + [4, 2, 1] |
| 54 | +]; |
| 55 | +const result = minPathSum(grid); |
| 56 | +console.log('Minimum path sum:', result); |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## Versión en Español |
| 62 | + |
| 63 | +### Descripción del Reto |
| 64 | +Dada una matriz de enteros, determina la suma mínima para llegar desde la esquina superior izquierda hasta la esquina inferior derecha, moviéndote solo a la derecha o hacia abajo. Usa programación dinámica para optimizar la solución. |
| 65 | + |
| 66 | +### Explicación del Código |
| 67 | +La solución utiliza una tabla 2D de programación dinámica `dp` donde `dp[i][j]` representa la suma mínima del camino para llegar a la celda `(i, j)`. |
| 68 | + |
| 69 | +La primera fila y la primera columna se inicializan acumulando sumas desde el inicio. Para otras celdas, la suma mínima del camino es el valor de la celda más el mínimo de las sumas del vecino superior o izquierdo. |
| 70 | + |
| 71 | +### Fragmento de Código Relevante |
| 72 | + |
| 73 | +```javascript |
| 74 | +function minPathSum(grid) { |
| 75 | + if (!grid || grid.length === 0 || grid[0].length === 0) { |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + const m = grid.length; |
| 80 | + const n = grid[0].length; |
| 81 | + const dp = Array.from({ length: m }, () => Array(n).fill(0)); |
| 82 | + |
| 83 | + dp[0][0] = grid[0][0]; |
| 84 | + |
| 85 | + for (let i = 1; i < m; i++) { |
| 86 | + dp[i][0] = dp[i - 1][0] + grid[i][0]; |
| 87 | + } |
| 88 | + |
| 89 | + for (let j = 1; j < n; j++) { |
| 90 | + dp[0][j] = dp[0][j - 1] + grid[0][j]; |
| 91 | + } |
| 92 | + |
| 93 | + for (let i = 1; i < m; i++) { |
| 94 | + for (let j = 1; j < n; j++) { |
| 95 | + dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return dp[m - 1][n - 1]; |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Ejemplo de Uso |
| 104 | + |
| 105 | +```javascript |
| 106 | +import minPathSum from './minPathSum.js'; |
| 107 | + |
| 108 | +const grid = [ |
| 109 | + [1, 3, 1], |
| 110 | + [1, 5, 1], |
| 111 | + [4, 2, 1] |
| 112 | +]; |
| 113 | +const result = minPathSum(grid); |
| 114 | +console.log('Suma mínima del camino:', result); |
0 commit comments