Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a4e71c

Browse files
feat: solve No.921,1289
1 parent 72dc185 commit 4a4e71c

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 1289. Minimum Falling Path Sum II
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Dynamic Programming, Matrix.
5+
- Similar Questions: Minimum Falling Path Sum.
6+
7+
## Problem
8+
9+
Given an `n x n` integer matrix `grid`, return **the minimum sum of a **falling path with non-zero shifts****.
10+
11+
A **falling path with non-zero shifts** is a choice of exactly one element from each row of `grid` such that no two elements chosen in adjacent rows are in the same column.
12+
13+
14+
Example 1:
15+
16+
![](https://assets.leetcode.com/uploads/2021/08/10/falling-grid.jpg)
17+
18+
```
19+
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
20+
Output: 13
21+
Explanation:
22+
The possible falling paths are:
23+
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
24+
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
25+
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
26+
The falling path with the smallest sum is [1,5,7], so the answer is 13.
27+
```
28+
29+
Example 2:
30+
31+
```
32+
Input: grid = [[7]]
33+
Output: 7
34+
```
35+
36+
37+
**Constraints:**
38+
39+
40+
41+
- `n == grid.length == grid[i].length`
42+
43+
- `1 <= n <= 200`
44+
45+
- `-99 <= grid[i][j] <= 99`
46+
47+
48+
49+
## Solution
50+
51+
```javascript
52+
/**
53+
* @param {number[][]} grid
54+
* @return {number}
55+
*/
56+
var minFallingPathSum = function(grid) {
57+
for (var i = 1; i < grid.length; i++) {
58+
var [minIndex, secondMinIndex] = findMinIndexs(grid[i - 1]);
59+
for (var j = 0; j < grid[i].length; j++) {
60+
grid[i][j] += grid[i - 1][minIndex === j ? secondMinIndex : minIndex];
61+
}
62+
}
63+
return Math.min(...grid[grid.length - 1]);
64+
};
65+
66+
var findMinIndexs = function(arr) {
67+
var minIndex = arr[0] > arr[1] ? 1 : 0;
68+
var secondMinIndex = arr[0] > arr[1] ? 0 : 1;
69+
for (var i = 2; i < arr.length; i++) {
70+
if (arr[i] < arr[minIndex]) {
71+
secondMinIndex = minIndex;
72+
minIndex = i;
73+
} else if (arr[i] < arr[secondMinIndex]) {
74+
secondMinIndex = i;
75+
}
76+
}
77+
return [minIndex, secondMinIndex];
78+
};
79+
```
80+
81+
**Explain:**
82+
83+
nope.
84+
85+
**Complexity:**
86+
87+
* Time complexity : O(n * m).
88+
* Space complexity : O(1).
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 931. Minimum Falling Path Sum
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming, Matrix.
5+
- Similar Questions: Minimum Falling Path Sum II.
6+
7+
## Problem
8+
9+
Given an `n x n` array of integers `matrix`, return **the **minimum sum** of any **falling path** through** `matrix`.
10+
11+
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)`.
12+
13+
14+
Example 1:
15+
16+
![](https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg)
17+
18+
```
19+
Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
20+
Output: 13
21+
Explanation: There are two falling paths with a minimum sum as shown.
22+
```
23+
24+
Example 2:
25+
26+
![](https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg)
27+
28+
```
29+
Input: matrix = [[-19,57],[-40,-5]]
30+
Output: -59
31+
Explanation: The falling path with a minimum sum is shown.
32+
```
33+
34+
35+
**Constraints:**
36+
37+
38+
39+
- `n == matrix.length == matrix[i].length`
40+
41+
- `1 <= n <= 100`
42+
43+
- `-100 <= matrix[i][j] <= 100`
44+
45+
46+
47+
## Solution
48+
49+
```javascript
50+
/**
51+
* @param {number[][]} matrix
52+
* @return {number}
53+
*/
54+
var minFallingPathSum = function(matrix) {
55+
for (var i = 1; i < matrix.length; i++) {
56+
for (var j = 0; j < matrix[i].length; j++) {
57+
matrix[i][j] += Math.min(
58+
j === 0 ? Number.MAX_SAFE_INTEGER : matrix[i - 1][j - 1],
59+
matrix[i - 1][j],
60+
j === matrix[i - 1].length - 1 ? Number.MAX_SAFE_INTEGER : matrix[i - 1][j + 1],
61+
);
62+
}
63+
}
64+
return Math.min(...matrix[matrix.length - 1]);
65+
};
66+
```
67+
68+
**Explain:**
69+
70+
nope.
71+
72+
**Complexity:**
73+
74+
* Time complexity : O(n * m).
75+
* Space complexity : O(n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /