Given an R × C 2D grid filled with non-negative integers, find a path from the top-left cell (0, 0) to the bottom-right cell (R - 1, C - 1) that minimizes the total sum of all numbers along its path.
At each step, you can only move right or down.
R and C (1 ≤ R, C ≤ 500), representing the number of rows and columns.R lines: Each line contains C space-separated non-negative integers representing the grid cell values (0 ≤ Grid[i][j] ≤ 1000).
(0, 0) to (R - 1, C - 1).Input:
3 3
1 3 1
1 5 1
4 2 1
Output:
7
Explanation:
The optimal path is (0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) with cell values 1 + 3 + 1 + 1 + 1 = 7.
Input:
2 3
1 2 3
4 5 6
Output:
12
Explanation:
The path (0, 0) → (0, 1) → (0, 2) → (1, 2) gives 1 + 2 + 3 + 6 = 12.
Case 1
2 3 1 2 3 4 5 6
12
Case 2
3 3 1 3 1 1 5 1 4 2 1
7
Input
2 3 1 2 3 4 5 6
Expected Output
12
Run or submit your code to see output here.