Skip to content

Commit df8bfae

Browse files
add 931
1 parent 57ca631 commit df8bfae

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ _If you like this project, please leave me a star._ ★
174174
|937|[Reorder Log Files](https://leetcode.com/problems/reorder-log-files/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_937.java) | |Easy|
175175
|935|[Knight Dialer](https://leetcode.com/problems/knight-dialer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_935.java) | |Medium|
176176
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | |Easy|
177+
|931|[Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_931.java) | |Medium|Dynamic Programming
177178
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | |Easy|
178179
|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | |Easy|
179180
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | |Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 931. Minimum Falling Path Sum
5+
*
6+
* Given a square array of integers A, we want the minimum sum of a falling path through A.
7+
* A falling path starts at any element in the first row, and chooses one element from each row.
8+
* The next row's choice must be in a column that is different from the previous row's column by at most one.
9+
*
10+
* Example 1:
11+
* Input: [[1,2,3],[4,5,6],[7,8,9]]
12+
* Output: 12
13+
* Explanation:
14+
* The possible falling paths are:
15+
* [1,4,7], [1,4,8], [1,5,7], [1,5,8], [1,5,9]
16+
* [2,4,7], [2,4,8], [2,5,7], [2,5,8], [2,5,9], [2,6,8], [2,6,9]
17+
* [3,5,7], [3,5,8], [3,5,9], [3,6,8], [3,6,9]
18+
* The falling path with the smallest sum is [1,4,7], so the answer is 12.
19+
*
20+
* Note:
21+
* 1 <= A.length == A[0].length <= 100
22+
* -100 <= A[i][j] <= 100
23+
* */
24+
public class _931 {
25+
public static class Solution1 {
26+
public int minFallingPathSum(int[][] A) {
27+
int size = A.length;
28+
int[][] dp = new int[size][size];
29+
for (int i = 0; i < size; i++) {
30+
for (int j = 0; j < size; j++) {
31+
if (i == 0) {
32+
dp[i][j] = A[i][j];
33+
} else {
34+
int lastRow = dp[i - 1][j];
35+
if (j - 1 >= 0) {
36+
lastRow = Math.min(dp[i - 1][j - 1], lastRow);
37+
}
38+
if (j + 1 < size) {
39+
lastRow = Math.min(dp[i - 1][j + 1], lastRow);
40+
}
41+
dp[i][j] = lastRow + A[i][j];
42+
}
43+
}
44+
}
45+
int minSum = Integer.MAX_VALUE;
46+
for (int i = 0; i < size; i++) {
47+
minSum = Math.min(minSum, dp[size - 1][i]);
48+
}
49+
return minSum;
50+
}
51+
}
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._931;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _931Test {
10+
private static _931.Solution1 solution1;
11+
private static int[][] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _931.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[][]{
21+
{1, 2, 3},
22+
{4, 5, 6},
23+
{7, 8, 9}
24+
};
25+
assertEquals(12, solution1.minFallingPathSum(A));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)