101 Logo
onenoughtone

Code Implementation

Minimum Path Sum Implementation

Below is the implementation of the minimum path sum:

solution.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Find the minimum path sum from top-left to bottom-right.
* Dynamic Programming (2D) approach.
*
* @param {number[][]} grid - Grid filled with non-negative integers
* @return {number} - Minimum path sum
*/
function minPathSum(grid) {
const m = grid.length;
const n = grid[0].length;
// Initialize DP array
const dp = Array(m).fill().map(() => Array(n).fill(0));
// Set base case for top-left cell
dp[0][0] = grid[0][0];
// Set base cases for first row
for (let j = 1; j < n; j++) {
dp[0][j] = grid[0][j] + dp[0][j - 1];
}
// Set base cases for first column
for (let i = 1; i < m; i++) {
dp[i][0] = grid[i][0] + dp[i - 1][0];
}
// Fill DP array
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[m - 1][n - 1];
}
/**
* Find the minimum path sum from top-left to bottom-right.
* Dynamic Programming (1D) approach.
*
* @param {number[][]} grid - Grid filled with non-negative integers
* @return {number} - Minimum path sum
*/
function minPathSumOptimized(grid) {
const m = grid.length;
const n = grid[0].length;
// Initialize DP array for first row
const dp = Array(n).fill(0);
dp[0] = grid[0][0];
// Set base cases for first row
for (let j = 1; j < n; j++) {
dp[j] = grid[0][j] + dp[j - 1];
}
// Fill DP array
for (let i = 1; i < m; i++) {
// Update first column
dp[0] = dp[0] + grid[i][0];
// Update rest of the row
for (let j = 1; j < n; j++) {
dp[j] = grid[i][j] + Math.min(dp[j], dp[j - 1]);
}
}
return dp[n - 1];
}
/**
* Find the minimum path sum from top-left to bottom-right.
* In-place Dynamic Programming approach.
*
* @param {number[][]} grid - Grid filled with non-negative integers
* @return {number} - Minimum path sum
*/
function minPathSumInPlace(grid) {
const m = grid.length;
const n = grid[0].length;
// Create a copy of the grid to avoid modifying the input
const dp = grid.map(row => [...row]);
// Set base cases for first row
for (let j = 1; j < n; j++) {
dp[0][j] = dp[0][j] + dp[0][j - 1];
}
// Set base cases for first column
for (let i = 1; i < m; i++) {
dp[i][0] = dp[i][0] + dp[i - 1][0];
}
// Fill grid
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = dp[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[m - 1][n - 1];
}
// Test cases
const grid1 = [
[1, 3, 1],
[1, 5, 1],
[4, 2, 1]
];
console.log(minPathSum(grid1)); // 7
console.log(minPathSumOptimized(grid1)); // 7
console.log(minPathSumInPlace(grid1)); // 7
const grid2 = [
[1, 2, 3],
[4, 5, 6]
];
console.log(minPathSum(grid2)); // 12
console.log(minPathSumOptimized(grid2)); // 12
console.log(minPathSumInPlace(grid2)); // 12

Step-by-Step Explanation

Let's break down the implementation:

  1. Initialize DP Array: Set up a dynamic programming array to keep track of the minimum path sum to reach each cell in the grid.
  2. Set Base Cases: Define the base cases for the top-left cell, the first row, and the first column.
  3. Fill DP Array: For each cell, calculate the minimum path sum by adding the value of the cell to the minimum of the path sums from the cell above and the cell to the left.
  4. Return Final Answer: Return the value in the bottom-right corner of the DP array, which represents the minimum path sum to reach the destination.
  5. Optimize Space Complexity: Reduce the space complexity by using a 1D array instead of a 2D array, or by modifying the input grid in-place.
ProblemSolutionCode
101 Logo
onenoughtone