Skip to content

Commit

Permalink
add 1289
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Dec 16, 2019
1 parent dfe209d commit f98158c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,5 @@ LeetCode
|1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/)|c|[c++](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.cpp)|[python](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.py)|[go](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.go)|[js](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.js)|[java](./src/1284-Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix/1284.java)|Hard|
|1286|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|c|[c++](./src/1286-Iterator-for-Combination/1286.cpp)|[python](./src/1286-Iterator-for-Combination/1286.py)|[go](./src/1286-Iterator-for-Combination/1286.go)|[js](./src/1286-Iterator-for-Combination/1286.js)|[java](./src/1286-Iterator-for-Combination/1286.java)|Easy|
|1287|[Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)|c|[c++](./src/1287-Element-Appearing-More-Than-25%-In-Sorted-Array/1287.cpp)|[python](./src/1287-Element-Appearing-More-Than-25%-In-Sorted-Array/1287.py)|[go](./src/1287-Element-Appearing-More-Than-25%-In-Sorted-Array/1287.go)|[js](./src/1287-Element-Appearing-More-Than-25%-In-Sorted-Array/1287.js)|[java](./src/1287-Element-Appearing-More-Than-25%-In-Sorted-Array/1287.java)|Easy|
|1288|[Remove Covered Intervals](https://leetcode.com/problems/remove-covered-intervals/)|c|[c++](./src/1288-Remove-Covered-Intervals/1288.cpp)|[python](./src/1288-Remove-Covered-Intervals/1288.py)|[go](./src/1288-Remove-Covered-Intervals/1288.go)|[js](./src/1288-Remove-Covered-Intervals/1288.js)|[java](./src/1288-Remove-Covered-Intervals/1288.java)|Medium|
|1288|[Remove Covered Intervals](https://leetcode.com/problems/remove-covered-intervals/)|c|[c++](./src/1288-Remove-Covered-Intervals/1288.cpp)|[python](./src/1288-Remove-Covered-Intervals/1288.py)|[go](./src/1288-Remove-Covered-Intervals/1288.go)|[js](./src/1288-Remove-Covered-Intervals/1288.js)|[java](./src/1288-Remove-Covered-Intervals/1288.java)|Medium|
|1289|[Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/)|c|[c++](./src/1289-Minimum-Falling-Path-Sum-II/1289.cpp)|[python](./src/1289-Minimum-Falling-Path-Sum-II/1289.py)|[go](./src/1289-Minimum-Falling-Path-Sum-II/1289.go)|[js](./src/1289-Minimum-Falling-Path-Sum-II/1289.js)|[java](./src/1289-Minimum-Falling-Path-Sum-II/1289.java)|Hard|
26 changes: 26 additions & 0 deletions src/1289-Minimum-Falling-Path-Sum-II/1289.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution
{
public:
int minFallingPathSum(vector<vector<int>>& A)
{
int r = A.size(), c = A[0].size();
for (int i = r - 1; i > 0; i--)
{
int m1[2] = {INT_MAX, 0}, m2[2] = {INT_MAX, 0};
for (int k = 0; k < c; k++)
{
if (A[i][k] < m1[0])
{
memcpy(m2, m1, sizeof m1);
m1[0] = A[i][k], m1[1] = k;
} else if (A[i][k] < m2[0]) m2[0] = A[i][k], m2[1] = k;
}

for (int j = 0; j < c; j++)
{
A[i-1][j] += (j != m1[1] ? m1[0] : m2[0]);
}
}
return *min_element(A[0].begin(), A[0].end());
}
};
35 changes: 35 additions & 0 deletions src/1289-Minimum-Falling-Path-Sum-II/1289.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
func minFallingPathSum(A [][]int) int {
r, c := len(A), len(A[0])
for i := r - 1; i > 0; i-- {
m1, m2 := []int{20000, 0}, []int{20000, 0}
for k := 0; k < c; k++ {
if A[i][k] < m1[0] {
copy(m2, m1)
m1[0], m1[1] = A[i][k], k
} else if A[i][k] < m2[0] {
m2[0], m2[1] = A[i][k], k
}
}

for j := 0; j < c; j++ {
if j != m1[1] {
A[i-1][j] += m1[0]
} else {
A[i-1][j] += m2[0]
}
}
}

res := 20000
for i := 0; i < c; i++ {
res = min(res, A[0][i])
}
return res
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
29 changes: 29 additions & 0 deletions src/1289-Minimum-Falling-Path-Sum-II/1289.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public int minFallingPathSum(int[][] A) {
int r = A.length, c = A[0].length;
for (int i = r - 1; i > 0; i--) {
int[] m1 = new int[]{20000, 0};
int[] m2 = new int[]{20000, 0};
for (int k = 0; k < c; k++) {
if (A[i][k] < m1[0]) {
m2 = m1.clone();
m1[0] = A[i][k];
m1[1] = k;
} else if (A[i][k] < m2[0]) {
m2[0] = A[i][k];
m2[1] = k;
}
}

for (int j = 0; j < c; j++) {
A[i-1][j] += (j != m1[1] ? m1[0] : m2[0]);
}
}

int res = 20000;
for (int i = 0; i < c; i++) {
res = Math.min(res, A[0][i]);
}
return res;
}
}
24 changes: 24 additions & 0 deletions src/1289-Minimum-Falling-Path-Sum-II/1289.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var minFallingPathSum = function(A) {
let r = A.length, c = A[0].length;
for (let i = r - 1; i > 0; i--) {
let m1 = [20000, 0], m2 = [20000, 0];
for (let k = 0; k < c; k++) {
if (A[i][k] < m1[0]) {
m2 = m1.slice();
m1 = [A[i][k], k];
} else if (A[i][k] < m2[0]) {
m2 = [A[i][k], k];
}
}

for (let j = 0; j < c; j++) {
A[i-1][j] += (j != m1[1] ? m1[0] : m2[0]);
}
}

let res = 20000;
for (let i = 0; i < c; i++) {
res = Math.min(res, A[0][i]);
}
return res;
};
8 changes: 8 additions & 0 deletions src/1289-Minimum-Falling-Path-Sum-II/1289.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def minFallingPathSum(self, A: List[List[int]]) -> int:
r, c = len(A), len(A[0])
for i in range(1, r):
r = heapq.nsmallest(2, A[i - 1])
for j in range(c):
A[i][j] += r[1] if A[i - 1][j] == r[0] else r[0]
return min(A[-1])
8 changes: 4 additions & 4 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os, bisect

# 题目名称
name = "Remove Covered Intervals"
ID = 1288
url = "https://leetcode.com/problems/remove-covered-intervals/"
difficult = "Medium"
name = "Minimum Falling Path Sum II"
ID = 1289
url = "https://leetcode.com/problems/minimum-falling-path-sum-ii/"
difficult = "Hard"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']


Expand Down

0 comments on commit f98158c

Please sign in to comment.