Skip to content

Commit 1bb4a97

Browse files
add 1289
1 parent df8bfae commit 1bb4a97

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ _If you like this project, please leave me a star._ ★
5050
|1295|[Find Numbers with Even Number of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1295.java) | [:tv:](https://youtu.be/HRp8mNJvLZ0) |Easy||
5151
|1291|[Sequential Digits](https://leetcode.com/problems/sequential-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1291.java) | |Medium||
5252
|1290|[Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1290.java) | |Easy||
53+
|1289|[Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1289.java) | |Hard|Dynamic Programming|
5354
|1287|[Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1287.java) | [:tv:](https://youtu.be/G74W8v2yVjY) |Easy||
5455
|1286|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1286.java) | |Medium|Backtracking, Design|
5556
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | [:tv:](https://www.youtube.com/watch?v=wGgcRCpSAa8)|Medium||
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1289. Minimum Falling Path Sum II
5+
*
6+
* Given a square grid of integers arr, a falling path with non-zero shifts is a
7+
* choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column.
8+
* Return the minimum sum of a falling path with non-zero shifts.
9+
*
10+
* Example 1:
11+
* Input: arr = [[1,2,3],[4,5,6],[7,8,9]]
12+
* Output: 13
13+
* Explanation:
14+
* The possible falling paths are:
15+
* [1,5,9], [1,5,7], [1,6,7], [1,6,8],
16+
* [2,4,8], [2,4,9], [2,6,7], [2,6,8],
17+
* [3,4,8], [3,4,9], [3,5,7], [3,5,9]
18+
* The falling path with the smallest sum is [1,5,7], so the answer is 13.
19+
*
20+
* Constraints:
21+
* 1 <= arr.length == arr[i].length <= 200
22+
* -99 <= arr[i][j] <= 99
23+
* */
24+
public class _1289 {
25+
public static class Solution1 {
26+
public int minFallingPathSum(int[][] arr) {
27+
int size = arr.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] = arr[i][j];
33+
} else {
34+
int previousRowMin = Integer.MAX_VALUE;
35+
for (int k = 0; k < size; k++) {
36+
if (k != j) {
37+
previousRowMin = Math.min(dp[i - 1][k], previousRowMin);
38+
}
39+
}
40+
dp[i][j] = arr[i][j] + previousRowMin;
41+
}
42+
}
43+
}
44+
int result = dp[size - 1][size - 1];
45+
for (int i = 0; i < size; i++) {
46+
result = Math.min(result, dp[size - 1][i]);
47+
}
48+
return result;
49+
}
50+
}
51+
}
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._1289;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1289Test {
10+
private static _1289.Solution1 solution1;
11+
private static int[][] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1289.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[][]{
21+
{1, 2, 3},
22+
{4, 5, 6},
23+
{7, 8, 9}
24+
};
25+
assertEquals(13, solution1.minFallingPathSum(arr));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy