The problem 81 of Project Euler gives a square matrix and requires one to find the minimal sum path. Specifically, it only required the sum of the numbers on the path. The question immediately reminded me of problem 67 and I set out solving with the same logic.
The logic has been explained in the following post of mine: Project Euler : Maximum Sum Traversing Top To Bottom In A Triangle.
The following lines of code did the work for me:
public static int getMinimalSumPath(int[][] numbers) {
int dimension = numbers.length - 1;
for (int x = dimension; x >= 0; x--)
for (int y = dimension; y >= 0; y--) {
if (x == dimension && y == dimension) continue;
int val1 = x < dimension ? numbers[x + 1][y] : Integer.MAX_VALUE;
int val2 = y < dimension ? numbers[x][y + 1] : Integer.MAX_VALUE;
numbers[x][y] += val1 < val2 ? val1 : val2;
}
return numbers[0][0];
}
Popularity: 13% [?]