Skip to content

Commit 0b4ffb5

Browse files
committed
Solve calculation of minimum path sum for a 2D array
1 parent 2974f4f commit 0b4ffb5

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.sean.recursive;
2+
3+
// 64. Minimum Path Sum
4+
public class MinPathFinder {
5+
// add a result cache
6+
private int[][] marks;
7+
8+
private int row;
9+
private int column;
10+
11+
public int minPathSum(int[][] grid) {
12+
if (grid == null || grid.length == 0)
13+
return 0;
14+
15+
if (grid.length == 1 && grid[0].length == 1)
16+
return grid[0][0];
17+
18+
// m
19+
row = grid.length;
20+
// n
21+
column = grid[0].length;
22+
23+
marks = new int[row][column];
24+
25+
int minSum = calcMinSum(grid, row - 1, column - 1);
26+
return minSum;
27+
}
28+
29+
private int calcMinSum(int[][] grid, int row, int column) {
30+
if (row < 0 || column < 0) {
31+
return 0;
32+
}
33+
34+
if (marks[row][column] != 0) {
35+
return marks[row][column];
36+
}
37+
38+
int sumMin = grid[row][column];
39+
if (row == 0 || column == 0) {
40+
if (row == 0 && column == 0) {
41+
return grid[0][0];
42+
}
43+
44+
if (row == 0) {
45+
return sumMin + calcMinSum(grid, row, column - 1);
46+
} else {
47+
// column == 0
48+
return sumMin + calcMinSum(grid, row - 1, column);
49+
}
50+
51+
}
52+
53+
int topResult = calcMinSum(grid, row - 1, column);
54+
int leftResult = calcMinSum(grid, row, column - 1);
55+
56+
int sum = sumMin + Math.min(leftResult, topResult);
57+
58+
marks[row][column] = sum;
59+
return sum;
60+
}
61+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.sean.recursive;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
public class MinPathFinderTest {
11+
12+
private MinPathFinder pathFinder;
13+
14+
@Before
15+
public void setup() {
16+
pathFinder = new MinPathFinder();
17+
}
18+
19+
@Test
20+
public void minPathSum() {
21+
22+
int[][] src = {
23+
{1, 3, 1},
24+
{1, 5, 1},
25+
{4, 2, 1}
26+
};
27+
28+
Assert.assertEquals(7, pathFinder.minPathSum(src));
29+
30+
// Not pass for this test case!!
31+
int[][] dat = {
32+
{1, 4, 8, 6, 2, 2, 1, 7},
33+
{4, 7, 3, 1, 4, 5, 5, 1},
34+
{8, 8, 2, 1, 1, 8, 0, 1},
35+
{8, 9, 2, 9, 8, 0, 8, 9},
36+
{5, 7, 5, 7, 1, 8, 5, 5},
37+
{7, 0, 9, 4, 5, 6, 5, 6},
38+
{4, 9, 9, 7, 9, 1, 9, 0}
39+
};
40+
Assert.assertEquals(47, pathFinder.minPathSum(dat));
41+
}
42+
43+
@Test
44+
public void testLargeData() {
45+
String str =
46+
"[7,1,3,5,8,9,9,2,1,9,0,8,3,1,6,6,9,5],\n" +
47+
"[9,5,9,4,0,4,8,8,9,5,7,3,6,6,6,9,1,6],\n" +
48+
"[8,2,9,1,3,1,9,7,2,5,3,1,2,4,8,2,8,8],\n" +
49+
"[6,7,9,8,4,8,3,0,4,0,9,6,6,0,0,5,1,4],\n" +
50+
"[7,1,3,1,8,8,3,1,2,1,5,0,2,1,9,1,1,4],\n" +
51+
"[9,5,4,3,5,6,1,3,6,4,9,7,0,8,0,3,9,9],\n" +
52+
"[1,4,2,5,8,7,7,0,0,7,1,2,1,2,7,7,7,4],\n" +
53+
"[3,9,7,9,5,8,9,5,6,9,8,8,0,1,4,2,8,2],\n" +
54+
"[1,5,2,2,2,5,6,3,9,3,1,7,9,6,8,6,8,3],\n" +
55+
"[5,7,8,3,8,8,3,9,9,8,1,9,2,5,4,7,7,7],\n" +
56+
"[2,3,2,4,8,5,1,7,2,9,5,2,4,2,9,2,8,7],\n" +
57+
"[0,1,6,1,1,0,0,6,5,4,3,4,3,7,9,6,1,9]";
58+
int beg = 0;
59+
int end = 0;
60+
61+
int lastEnd = end;
62+
List<String> digits = new LinkedList<>();
63+
64+
while (beg >= 0) {
65+
beg = str.indexOf("[", lastEnd);
66+
if (beg >= 0) {
67+
end = str.indexOf("]", beg);
68+
lastEnd = end;
69+
70+
digits.add(str.substring(beg + 1, end));
71+
}
72+
}
73+
System.out.println(digits);
74+
75+
int rowCnt = digits.size();
76+
int colCnt = digits.get(0).split(",").length;
77+
int[][] src = new int[rowCnt][colCnt];
78+
79+
for (int i = 0; i < digits.size(); i++) {
80+
String[] array = digits.get(i).split(",");
81+
82+
for (int j = 0; j < colCnt; j++) {
83+
src[i][j] = Integer.parseInt(array[j]);
84+
}
85+
}
86+
87+
int len = pathFinder.minPathSum(src);
88+
89+
Assert.assertEquals(85, len);
90+
}
91+
}

0 commit comments

Comments
 (0)