Skip to content

Commit 03d6688

Browse files
add 931
1 parent 8e4f735 commit 03d6688

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,5 @@ LeetCode
170170
|0926|[Flip String to Monotone Increasing](https://leetcode.com/problems/flip-string-to-monotone-increasing/) | c | [c++](./src/0926-Flip-String-to-Monotone-Increasing/0926.cpp) |[python](./src/0926-Flip-String-to-Monotone-Increasing/0926.py)|||Medium|
171171
|0927|[Three Equal Parts](https://leetcode.com/problems/three-equal-parts/) | c | [c++](./src/0927-Three-Equal-Parts/0927.cpp) |[python](./src/0927-Three-Equal-Parts/0927.py)|||Medium|
172172
|0929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | c | [c++](./src/0925-Long-Pressed-Name/0925.cpp) |[python](./src/0925-Long-Pressed-Name/0925.py)|||Easy|
173-
|0930|[Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | c | [c++](./src/0927-Three-Equal-Parts/0927.cpp) |[python](./src/0927-Three-Equal-Parts/0927.py)|||Medium|
173+
|0930|[Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | c | [c++](./src/0927-Three-Equal-Parts/0927.cpp) |[python](./src/0927-Three-Equal-Parts/0927.py)|||Medium|
174+
|0931|[Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | c | [c++](./src/0927-Three-Equal-Parts/0927.cpp) |[python](./src/0927-Three-Equal-Parts/0927.py)|||Medium|
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
7+
class Solution
8+
{
9+
public:
10+
int minFallingPathSum(vector<vector<int>>& A)
11+
{
12+
if (A.empty()) return 0;
13+
unsigned int r = A.size(), c = A[0].size();
14+
for (int row = r - 1; row > 0; --row)
15+
{
16+
for (unsigned int col = 0; col < c; ++col)
17+
{
18+
if (col == 0)
19+
A[row - 1][col] += min(A[row][col], A[row][col + 1]);
20+
else if (col == c - 1)
21+
A[row - 1][col] += min(A[row][col], A[row][col - 1]);
22+
else A[row - 1][col] += min(A[row][col], min(A[row][col + 1], A[row][col - 1]));
23+
}
24+
}
25+
return *min_element(A[0].begin(), A[0].end());
26+
}
27+
};
28+
int main()
29+
{
30+
vector<vector<int>> nums = {{-19,57},{-40,-5}};
31+
cout << Solution().minFallingPathSum(nums);
32+
return 0;
33+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def minFallingPathSum(self, A):
3+
"""
4+
:type A: List[List[int]]
5+
:rtype: int
6+
"""
7+
if not A:
8+
return 0
9+
10+
r, c = len(A), len(A[0])
11+
for row in range(r - 1, 0, -1):
12+
for col in range(c):
13+
if col == 0:
14+
A[row - 1][col] += min(A[row][col], A[row][col + 1])
15+
elif col == c - 1:
16+
A[row - 1][col] += min(A[row][col], A[row][col - 1])
17+
else:
18+
A[row - 1][col] += min(A[row][col], A[row][col + 1], A[row][col - 1])
19+
20+
return min(A[0])
21+
22+
if __name__ == "__main__":
23+
A = [[-19,57],[-40,-5]]
24+
print(Solution().minFallingPathSum(A))

0 commit comments

Comments
 (0)