-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathMinimumPath.java
55 lines (45 loc) · 1.94 KB
/
MinimumPath.java
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
import java.util.*;
class MinimumPath {
// Function to calculate the minimum sum path in the matrix
static int minSumPath(int n, int m, int[][] matrix) {
// Initialize an array to store the previous row values
int prev[] = new int[m];
for (int i = 0; i < n; i++) {
// Create a temporary array to store the current row values
int temp[] = new int[m];
for (int j = 0; j < m; j++) {
if (i == 0 && j == 0)
temp[j] = matrix[i][j]; // If we're at the top-left cell, the minimum sum is its value
else {
int up = matrix[i][j];
if (i > 0)
up += prev[j]; // Add the value from above if it's not out of bounds
else
up += (int) Math.pow(10, 9); // Add a large value if out of bounds in the up direction
int left = matrix[i][j];
if (j > 0)
left += temp[j - 1]; // Add the value from the left if it's not out of bounds
else
left += (int) Math.pow(10, 9); // Add a large value if out of bounds in the left direction
// Store the minimum of the two possible paths in the current cell
temp[j] = Math.min(up, left);
}
}
// Update the previous row with the values of the current row
prev = temp;
}
// The final result is stored in the last element of the previous row
return prev[m - 1];
}
public static void main(String args[]) {
// Define the matrix
int matrix[][] = {
{5, 9, 6},
{11, 5, 2}
};
int n = matrix.length;
int m = matrix[0].length;
// Calculate and print the minimum sum path in the matrix
System.out.println(minSumPath(n, m, matrix));
}
}