|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 931. Minimum Falling Path Sum *- |
| 4 | +
|
| 5 | +
|
| 6 | +Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix. |
| 7 | +
|
| 8 | +A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1). |
| 9 | +
|
| 10 | + |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +
|
| 14 | +
|
| 15 | +Input: matrix = [[2,1,3],[6,5,4],[7,8,9]] |
| 16 | +Output: 13 |
| 17 | +Explanation: There are two falling paths with a minimum sum as shown. |
| 18 | +Example 2: |
| 19 | +
|
| 20 | +
|
| 21 | +Input: matrix = [[-19,57],[-40,-5]] |
| 22 | +Output: -59 |
| 23 | +Explanation: The falling path with a minimum sum is shown. |
| 24 | + |
| 25 | +
|
| 26 | +Constraints: |
| 27 | +
|
| 28 | +n == matrix.length == matrix[i].length |
| 29 | +1 <= n <= 100 |
| 30 | +-100 <= matrix[i][j] <= 100 |
| 31 | +
|
| 32 | +
|
| 33 | +
|
| 34 | +*/ |
| 35 | + |
| 36 | +import 'dart:math'; |
| 37 | + |
| 38 | +/* |
| 39 | +
|
| 40 | +Approach: This problem has an optimal substructure, meaning that the solutions to sub-problems can be used to solve larger instances of this problem. This makes dynamic programming came into existence. |
| 41 | +
|
| 42 | +Let dp[R][C] be the minimum total weight of a falling path starting at [R, C] in first row and reaching to the bottom row of A. |
| 43 | +Then, dp[R][C] = A[R][C] + min(dp[R+1, C-1], dp[R+1, C], dp[R+1, C+1]) , and the answer is minimum value of first row i:e \underset{C}{min}\; dp(0, C) . |
| 44 | +We would make an auxiliary array dp to cache intermediate values dp[R][C]. However, we will use A to cache these values. Our goal is to transform the values of A into the values of dp. |
| 45 | +We begins processing each row, starting with the second last row. We set A[R][C] = min(A[R+1, C-1], A[R+1, C], A[R+1, C+1]) , handling boundary conditions gracefully. |
| 46 | +*/ |
| 47 | +class A { |
| 48 | + // Bottom UP O(n2) |
| 49 | + int minFallingPathSum(List<List<int>> matrix) { |
| 50 | + // BASE CASE:- if the matrix is empty |
| 51 | + if (matrix.length == 0) return 0; |
| 52 | + // 2D :- Matrix with fixed length |
| 53 | + // COLUMN :- matrix.length |
| 54 | + // ROW :- matrix[0].length |
| 55 | + List<List<int>> dp = List.filled(matrix.length, 0) |
| 56 | + .map((e) => List.filled(matrix[0].length, 0)) |
| 57 | + .toList(); |
| 58 | + // Height matrix |
| 59 | + int m = matrix.length; |
| 60 | + // width of the matrix |
| 61 | + int n = matrix[0].length; |
| 62 | + for (int i = 0; i < n; i++) { |
| 63 | + dp[m - 1][i] = matrix[m - 1][i]; |
| 64 | + } |
| 65 | + |
| 66 | + for (int i = m - 2; i >= 0; i--) { |
| 67 | + for (int j = 0; j < n; j++) { |
| 68 | + int mini = double.maxFinite.toInt(); |
| 69 | + if (j < n - 1) mini = min(dp[i + 1][j + 1], mini); |
| 70 | + if (j > 0) mini = min(dp[i + 1][j - 1], mini); |
| 71 | + mini = min(dp[i + 1][j], mini); |
| 72 | + |
| 73 | + dp[i][j] = matrix[i][j] + mini; |
| 74 | + } |
| 75 | + } |
| 76 | + int mini = double.maxFinite.toInt(); |
| 77 | + for (int i = 0; i < n; i++) mini = min(dp[0][i], mini); |
| 78 | + |
| 79 | + return mini; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class B { |
| 84 | + // Top Down O(n2) |
| 85 | + int minFallingPathSum(List<List<int>> matrix) { |
| 86 | + int rows = matrix.length; |
| 87 | + int columns = matrix[0].length; |
| 88 | + List<List<int>> dp = |
| 89 | + List.filled(rows, 0).map((e) => List.filled(columns, 0)).toList(); |
| 90 | + int ans = double.maxFinite.toInt(); |
| 91 | + for (int column = 0; column < columns; column += 1) { |
| 92 | + ans = min(ans, minPathSum(rows - 1, column, matrix, dp)); |
| 93 | + } |
| 94 | + return ans; |
| 95 | + } |
| 96 | + |
| 97 | + int minPathSum( |
| 98 | + int row, int column, List<List<int>> matrix, List<List<int>> dp) { |
| 99 | + if (row < 0) { |
| 100 | + return 0; |
| 101 | + } |
| 102 | + if (column < 0 || column >= matrix[0].length) { |
| 103 | + return 100000000; |
| 104 | + } |
| 105 | + if (dp[row][column] != 0) { |
| 106 | + return dp[row][column]; |
| 107 | + } |
| 108 | + int ans = matrix[row][column] + |
| 109 | + min( |
| 110 | + minPathSum(row - 1, column - 1, matrix, dp), |
| 111 | + min( |
| 112 | + minPathSum(row - 1, column, matrix, dp), |
| 113 | + minPathSum(row - 1, column + 1, matrix, dp), |
| 114 | + ), |
| 115 | + ); |
| 116 | + return dp[row][column] = ans; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class C { |
| 121 | + // int n = 3; |
| 122 | + int minFallingPathSum(List<List<int>> matrix) { |
| 123 | + int n = matrix.length; |
| 124 | + if (n == 1) return matrix[0][0]; |
| 125 | + int mini = double.maxFinite.toInt(); |
| 126 | + for (int i = 1; i < n; i++) { |
| 127 | + for (int j = 0; j < n; j++) { |
| 128 | + if (j == 0) |
| 129 | + matrix[i][j] += min(matrix[i - 1][j], matrix[i - 1][j + 1]); |
| 130 | + else if (j == n - 1) |
| 131 | + matrix[i][j] += min(matrix[i - 1][j], matrix[i - 1][j - 1]); |
| 132 | + else |
| 133 | + matrix[i][j] += min(matrix[i - 1][j], |
| 134 | + min(matrix[i - 1][j - 1], matrix[i - 1][j + 1])); |
| 135 | + if (i == n - 1) mini = min(mini, matrix[i][j]); |
| 136 | + } |
| 137 | + } |
| 138 | + return mini; |
| 139 | + } |
| 140 | +} |
0 commit comments