forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinFallingPathSum.java
More file actions
67 lines (62 loc) · 1.89 KB
/
MinFallingPathSum.java
File metadata and controls
67 lines (62 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
/**
* Implementation of the Minimum Falling Path Sum problem using Dynamic
* Programming.
*
* Given an n x n integer matrix, the algorithm finds the minimum sum of any
* falling path through the matrix. A falling path starts at any element in the
* first row and chooses one element from each row below, such that the next
* element is in the same column or an adjacent column.
*
* <p>Example:
* Input:
* 3
* 2 1 3
* 6 5 4
* 7 8 9
*
* Output:
* 13
*
* <p>Approach: Bottom-up Dynamic Programming
* Time Complexity: O(n^2)
* Space Complexity: O(n^2)
*
* @see <a
* href="https://leetcode.com/problems/minimum-falling-path-sum/">LeetCode:
* Minimum Falling Path Sum</a>
* @see <a href="https://en.wikipedia.org/wiki/Dynamic_programming">Wikipedia:
* Dynamic Programming</a>
*/
package dp;
import java.util.*;
public class MinFallingPathSum {
public static int minFallingPathSum(int[][] matrix) {
int n = matrix.length;
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++)
dp[n - 1][i] = matrix[n - 1][i];
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n; j++) {
int downLeft =
(i + 1 < n && j - 1 >= 0) ? dp[i + 1][j - 1] : Integer.MAX_VALUE;
int downRight =
(i + 1 < n && j + 1 < n) ? dp[i + 1][j + 1] : Integer.MAX_VALUE;
int down = (i + 1 < n) ? dp[i + 1][j] : Integer.MAX_VALUE;
dp[i][j] = matrix[i][j] + Math.min(down, Math.min(downLeft, downRight));
}
}
int mini = Integer.MAX_VALUE;
for (int i = 0; i < n; i++)
mini = Math.min(mini, dp[0][i]);
return mini;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = sc.nextInt();
System.out.println(minFallingPathSum(matrix));
}
}