Skip to content

Commit badf852

Browse files
committed
Add solution #1289
1 parent 756ed1a commit badf852

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,211 LeetCode solutions in JavaScript
1+
# 1,212 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -973,6 +973,7 @@
973973
1286|[Iterator for Combination](./solutions/1286-iterator-for-combination.js)|Medium|
974974
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
975975
1288|[Remove Covered Intervals](./solutions/1288-remove-covered-intervals.js)|Medium|
976+
1289|[Minimum Falling Path Sum II](./solutions/1289-minimum-falling-path-sum-ii.js)|Hard|
976977
1290|[Convert Binary Number in a Linked List to Integer](./solutions/1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
977978
1291|[Sequential Digits](./solutions/1291-sequential-digits.js)|Medium|
978979
1292|[Maximum Side Length of a Square with Sum Less than or Equal to Threshold](./solutions/1292-maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 1289. Minimum Falling Path Sum II
3+
* https://leetcode.com/problems/minimum-falling-path-sum-ii/
4+
* Difficulty: Hard
5+
*
6+
* Given an n x n integer matrix grid, return the minimum sum of a falling path with non-zero
7+
* shifts.
8+
*
9+
* A falling path with non-zero shifts is a choice of exactly one element from each row of grid
10+
* such that no two elements chosen in adjacent rows are in the same column.
11+
*/
12+
13+
/**
14+
* @param {number[][]} grid
15+
* @return {number}
16+
*/
17+
var minFallingPathSum = function(grid) {
18+
const n = grid.length;
19+
let prevRow = [...grid[0]];
20+
21+
for (let row = 1; row < n; row++) {
22+
const currRow = new Array(n);
23+
for (let col = 0; col < n; col++) {
24+
let minPrev = Infinity;
25+
for (let prevCol = 0; prevCol < n; prevCol++) {
26+
if (prevCol !== col) {
27+
minPrev = Math.min(minPrev, prevRow[prevCol]);
28+
}
29+
}
30+
currRow[col] = grid[row][col] + minPrev;
31+
}
32+
prevRow = currRow;
33+
}
34+
35+
return Math.min(...prevRow);
36+
};

0 commit comments

Comments
 (0)