-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFallingPathMinSum.java
More file actions
58 lines (52 loc) · 1.73 KB
/
Copy pathFallingPathMinSum.java
File metadata and controls
58 lines (52 loc) · 1.73 KB
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
/*Given a square array of integers A, we want the minimum sum of
a falling path through A. A falling path starts at any element
in the first row, and chooses one element from each row.
The next row’s choice must be in a column that is different
from the previous row’s column by at most one.
INPUT: [[1,2,3][4,5,6][7,8,9]]
OUTPUT: 12
*/
import java.util.Scanner;
public class FallingPathMinSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // Order of matrix
int[][] matrix = new int[n][n]; // Input Matrix
int[] fallingPathSum = new int[n]; // Array to store all falling paths
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < n; i++) {
fallingPathSum[i] = calculateFallingSum(matrix, i, n);
}
int minSum = minimumInArray(fallingPathSum); //To store the minimum falling paths available
System.out.println(minSum);
}
// Method to find minimum in a array
protected static int minimumInArray(int[] arr) {
int minimum = arr[0];
for (int j : arr) {
if (j < minimum) {
minimum = j;
}
}
return minimum;
}
private static int calculateFallingSum(int[][] arr, int index, int n) {
int sum = arr[0][index];
int j = 0;
if (index == 0) {
j = index + 1;
}
if (index == n - 1) {
j = n - 2;
}
for (int i = 1; i < arr.length; i++) {
for (; j < arr.length; j++) {
}
}
return sum;
}
}