File tree 2 files changed +38
-1
lines changed
2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,211 LeetCode solutions in JavaScript
1
+ # 1,212 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
973
973
1286|[ Iterator for Combination] ( ./solutions/1286-iterator-for-combination.js ) |Medium|
974
974
1287|[ Element Appearing More Than 25% In Sorted Array] ( ./solutions/1287-element-appearing-more-than-25-in-sorted-array.js ) |Easy|
975
975
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|
976
977
1290|[ Convert Binary Number in a Linked List to Integer] ( ./solutions/1290-convert-binary-number-in-a-linked-list-to-integer.js ) |Easy|
977
978
1291|[ Sequential Digits] ( ./solutions/1291-sequential-digits.js ) |Medium|
978
979
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|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments