forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfe209d
commit f98158c
Showing
7 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters