What if you could find the cheapest path through a maze of streets without trying every route?
Why Minimum Path Sum in Grid in DSA C#?
Imagine you are walking through a city grid where each block has a cost to cross. You want to find the cheapest way from the top-left corner to the bottom-right corner, moving only right or down.
Trying every possible path manually is overwhelming because the number of paths grows very fast. It's easy to get lost, make mistakes, or waste time calculating the same routes again and again.
The Minimum Path Sum method uses a smart way to remember the cheapest cost to reach each block. It builds the answer step-by-step, so you don't repeat work and quickly find the cheapest path.
int minSum = int.MaxValue;
void Explore(int row, int col, int currentSum) {
if (row == grid.Length - 1 && col == grid[0].Length - 1) {
minSum = Math.Min(minSum, currentSum);
return;
}
if (row + 1 < grid.Length) Explore(row + 1, col, currentSum + grid[row + 1][col]);
if (col + 1 < grid[0].Length) Explore(row, col + 1, currentSum + grid[row][col + 1]);
}int[,] dp = new int[rows, cols]; dp[0,0] = grid[0][0]; for (int i = 1; i < rows; i++) dp[i,0] = dp[i-1,0] + grid[i][0]; for (int j = 1; j < cols; j++) dp[0,j] = dp[0,j-1] + grid[0][j]; for (int i = 1; i < rows; i++) for (int j = 1; j < cols; j++) dp[i,j] = Math.Min(dp[i-1,j], dp[i,j-1]) + grid[i][j];
This method lets you quickly find the cheapest path through any grid, saving time and avoiding mistakes.
Delivery drivers use similar calculations to find the quickest or cheapest route through city streets to save fuel and time.
Manual path checking is slow and error-prone.
Dynamic programming remembers costs to avoid repeated work.
Efficiently finds the minimum cost path in a grid.
Practice
Given a grid of numbers, find the path from top-left to bottom-right with the smallest sum.Solution
Step 1: Understand the problem goal
The problem asks to find a path from the top-left corner to the bottom-right corner of the grid that has the smallest sum of all numbers along the path.Step 2: Clarify path movement
Movement is only allowed down or right, so the path must be chosen to minimize the total sum.Final Answer:
The path with the smallest sum of numbers from top-left to bottom-right -> Option DQuick Check:
Minimum path sum means smallest total sum [OK]
- Confusing minimum with maximum sum
- Counting number of paths instead of sum
- Ignoring movement restrictions (only down/right)
Solution
Step 1: Identify correct 2D array syntax in C#
In C#, a rectangular 2D array is declared asint[,] grid = new int[rows, columns];and initialized with nested braces.Step 2: Check each option's syntax
int[,] grid = new int[3, 3] { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} }; usesint[,]with proper initialization. Options A, C, and D incorrectly mix jagged and rectangular array syntax. int[,] grid = new int[3][3] { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} }; uses invalid syntax for rectangular arrays.Final Answer:
int[,] grid = new int[3, 3] { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} }; -> Option CQuick Check:
Rectangular array syntax = int[,] grid = new int[3, 3] { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} }; [OK]
- Using jagged array syntax with rectangular arrays
- Mixing [] and [,] incorrectly
- Incorrect initialization braces
int[,] grid = { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} };
int rows = grid.GetLength(0);
int cols = grid.GetLength(1);
int[,] dp = new int[rows, cols];
dp[0, 0] = grid[0, 0];
for (int i = 1; i < rows; i++) dp[i, 0] = dp[i - 1, 0] + grid[i, 0];
for (int j = 1; j < cols; j++) dp[0, j] = dp[0, j - 1] + grid[0, j];
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + grid[i, j];
}
}
Console.WriteLine(dp[rows - 1, cols - 1]);Solution
Step 1: Initialize dp array edges
dp first row: dp[0,0]=1, dp[0,1]=1+3=4, dp[0,2]=4+1=5
dp first column: dp[1,0]=1+1=2, dp[2,0]=2+4=6Step 2: Fill dp inner cells
dp[1,1] = min(dp[0,1], dp[1,0]) + 5 = min(4,2)+5=7
dp[1,2] = min(dp[0,2], dp[1,1]) + 1 = min(5,7)+1=6
dp[2,1] = min(dp[1,1], dp[2,0]) + 2 = min(7,6)+2=8
dp[2,2] = min(dp[1,2], dp[2,1]) + 1 = min(6,8)+1=7Final Answer:
7 -> Option AQuick Check:
Minimum path sum = 7 [OK]
- Not initializing first row and column correctly
- Using max instead of min
- Adding wrong grid values
int[,] grid = { {1, 2}, {1, 1} };
int rows = grid.GetLength(0);
int cols = grid.GetLength(1);
int[,] dp = new int[rows, cols];
dp[0, 0] = grid[0, 0];
for (int i = 1; i < rows; i++) dp[i, 0] = dp[i - 1, 0] + grid[i, 0];
for (int j = 1; j < cols; j++) dp[0, j] = dp[0, j - 1] + grid[0, j];
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]) + grid[i, j];
}
}
Console.WriteLine(dp[rows - 1, cols - 1]);Solution
Step 1: Review dp calculation logic
The problem requires minimum path sum, so dp[i,j] should use Math.Min to choose the smaller previous sum.Step 2: Identify incorrect function usage
The code uses Math.Max which picks the larger sum, causing incorrect results.Final Answer:
Using Math.Max instead of Math.Min in dp calculation -> Option BQuick Check:
Minimum path sum needs Math.Min, not Math.Max [OK]
- Using max instead of min in dp update
- Forgetting to initialize dp edges
- Wrong loop boundaries
int[,] grid = {
{1, 3, 1, 2},
{1, 5, 1, 1},
{4, 2, 1, 3}
};Write the minimum path sum from top-left to bottom-right if you can only move right or down. What is the sum?
Solution
Step 1: Initialize dp edges
First row sums: 1, 4(1+3), 5(4+1), 7(5+2)
First column sums: 1, 2(1+1), 6(2+4)Step 2: Fill dp inner cells
dp[1,1] = min(4,2)+5=7
dp[1,2] = min(5,7)+1=6
dp[1,3] = min(7,6)+1=7
dp[2,1] = min(7,6)+2=8
dp[2,2] = min(6,8)+1=7
dp[2,3] = min(7,7)+3=10Step 3: Find minimum path sum
The bottom-right cell dp[2,3] = 10 is the minimum path sum.Final Answer:
10 -> Option AQuick Check:
Minimum path sum calculated using dp = 10 [OK]
- Not updating dp correctly for inner cells
- Mixing up row and column indices
- Forgetting movement restrictions
Ready to prove it?
Pass the test and this pattern moves unknown
