Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e2c5cdf

Browse files
feat(js): add modular solution and documentation for normal challenge 8 - Minimum Path Sum
1 parent 661da72 commit e2c5cdf

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// main.js - Example usage of minPathSum function
2+
3+
import minPathSum from './minPathSum.js';
4+
5+
/*
6+
Challenge: 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+
9+
function main() {
10+
const grid = [
11+
[1, 3, 1],
12+
[1, 5, 1],
13+
[4, 2, 1]
14+
];
15+
const result = minPathSum(grid);
16+
console.log('Minimum path sum:', result);
17+
}
18+
19+
main();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// minPathSum.js - Calculate minimum path sum in a grid using dynamic programming
2+
3+
function minPathSum(grid) {
4+
if (!grid || grid.length === 0 || grid[0].length === 0) {
5+
return 0;
6+
}
7+
8+
const m = grid.length;
9+
const n = grid[0].length;
10+
const dp = Array.from({ length: m }, () => Array(n).fill(0));
11+
12+
dp[0][0] = grid[0][0];
13+
14+
for (let i = 1; i < m; i++) {
15+
dp[i][0] = dp[i - 1][0] + grid[i][0];
16+
}
17+
18+
for (let j = 1; j < n; j++) {
19+
dp[0][j] = dp[0][j - 1] + grid[0][j];
20+
}
21+
22+
for (let i = 1; i < m; i++) {
23+
for (let j = 1; j < n; j++) {
24+
dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
25+
}
26+
}
27+
28+
return dp[m - 1][n - 1];
29+
}
30+
31+
export default minPathSum;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /