-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumTwoWays.java
More file actions
56 lines (46 loc) · 1.65 KB
/
PathSumTwoWays.java
File metadata and controls
56 lines (46 loc) · 1.65 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
package dev;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PathSumTwoWays {
public static void main(String[] args) throws IOException {
int[][] matrix = loadMatrix("0081_matrix.txt");
int minimalPathSum = computeMinimalPathSum(matrix);
System.out.println("Minimal path sum: " + minimalPathSum);
}
private static int[][] loadMatrix(String fileName) throws IOException {
List<int[]> rows = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
int[] row = new int[values.length];
for (int i = 0; i < values.length; i++) {
row[i] = Integer.parseInt(values[i].trim());
}
rows.add(row);
}
br.close();
return rows.toArray(new int[0][]);
}
private static int computeMinimalPathSum(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] dp = new int[rows][cols];
dp[0][0] = matrix[0][0];
for (int j = 1; j < cols; j++) {
dp[0][j] = dp[0][j - 1] + matrix[0][j];
}
for (int i = 1; i < rows; i++) {
dp[i][0] = dp[i - 1][0] + matrix[i][0];
}
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]) + matrix[i][j];
}
}
return dp[rows - 1][cols - 1];
}
}