Skip to content

Commit b5ab246

Browse files
committed
no message
1 parent 4e98492 commit b5ab246

File tree

1 file changed

+70
-35
lines changed

1 file changed

+70
-35
lines changed
Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import Foundation
2-
class Solution {
3-
func minimumEffortPath(_ heights: [[Int]]) -> Int {
4-
guard !heights.isEmpty, !heights[0].isEmpty else {
5-
return 0
6-
}
7-
8-
var visited = Set<String>()
9-
visited.insert("0-0")
10-
var (path, minPath) = ([heights[0][0]], heights[0][0])
11-
12-
minimumEffortPathDFSHelper(heights, path, &minPath, 0, 0, &visited)
13-
return minPath
14-
}
15-
16-
func minimumEffortPathDFSHelper(_ heights: [[Int]], _ path: [Int], _ minPath: inout Int, _ currentX: Int, _ currentY: Int, _ visited: inout Set<String>) {
17-
print("currentX: \(currentX), currentY: \(currentY)")
18-
if currentX == heights.count - 1 && currentY == heights[0].count - 1 {
19-
minPath = min(minPath, path.max()!)
20-
print("path: \(path)")
21-
return
22-
}
23-
print("------")
24-
for neighbor in [(-1,0),(0,1),(1,0),(0,-1)] {
25-
var (newX, newY) = (currentX + neighbor.0, currentY + neighbor.1)
26-
if newX >= 0 && newX < heights.count && newY >= 0 && newY < heights[newX].count && !visited.contains("\(newX)-\(newY)") {
27-
var currVal = heights[newX][newY]
28-
var prevVal = heights[currentX][currentY]
29-
30-
visited.insert("\(newX)-\(newY)")
31-
minimumEffortPathDFSHelper(heights, path + [abs(prevVal - currVal)], &minPath, newX, newY, &visited)
32-
visited.remove("\(newX)-\(newY)")
33-
}
34-
}
35-
}
36-
}
2+
//class Solution {
3+
// func minimumEffortPath(_ heights: [[Int]]) -> Int {
4+
// guard !heights.isEmpty, !heights[0].isEmpty else {
5+
// return 0
6+
// }
7+
//
8+
// var visited = Set<String>()
9+
// visited.insert("0-0")
10+
// var (path, minPath) = ([heights[0][0]], heights[0][0])
11+
//
12+
// minimumEffortPathDFSHelper(heights, path, &minPath, 0, 0, &visited)
13+
// return minPath
14+
// }
15+
//
16+
// func minimumEffortPathDFSHelper(_ heights: [[Int]], _ path: [Int], _ minPath: inout Int, _ currentX: Int, _ currentY: Int, _ visited: inout Set<String>) {
17+
// print("currentX: \(currentX), currentY: \(currentY)")
18+
// if currentX == heights.count - 1 && currentY == heights[0].count - 1 {
19+
// minPath = min(minPath, path.max()!)
20+
// print("path: \(path)")
21+
// return
22+
// }
23+
// print("------")
24+
// for neighbor in [(-1,0),(0,1),(1,0),(0,-1)] {
25+
// var (newX, newY) = (currentX + neighbor.0, currentY + neighbor.1)
26+
// if newX >= 0 && newX < heights.count && newY >= 0 && newY < heights[newX].count && !visited.contains("\(newX)-\(newY)") {
27+
// var currVal = heights[newX][newY]
28+
// var prevVal = heights[currentX][currentY]
29+
//
30+
// visited.insert("\(newX)-\(newY)")
31+
// minimumEffortPathDFSHelper(heights, path + [abs(prevVal - currVal)], &minPath, newX, newY, &visited)
32+
// visited.remove("\(newX)-\(newY)")
33+
// }
34+
// }
35+
// }
36+
//}
3737

3838
/*
3939
[
@@ -43,3 +43,38 @@ class Solution {
4343
[1,2,1,2,1],
4444
[1,1,1,2,1]]
4545
*/
46+
47+
48+
49+
class Solution {
50+
func numSubmat(_ mat: [[Int]]) -> Int {
51+
guard !mat.isEmpty && !mat.first?.isEmpty else {
52+
return 0
53+
}
54+
var dp = Array(repeating: Array(repeating: 0, count: mat.first?.count + 1), count: mat.count + 1)
55+
var numSubmat = 0
56+
for i in 1...mat.count {
57+
for j in 1...mat.first?.count {
58+
if mat[i - 1][j - 1] == 1 {
59+
var currentCount = 0
60+
if dp[i - 1][j] == 1 {
61+
currentCount += dp[i - 1][j]
62+
currentCount += 1
63+
}
64+
if dp[i - 1][j - 1] == 1 {
65+
currentCount += dp[i - 1][j - 1]
66+
currentCount += 1
67+
}
68+
if dp[i][j - 1] == 1 {
69+
currentCount += dp[i][j - 1]
70+
currentCount += 1
71+
}
72+
73+
dp[i][j] = currentCount
74+
numSubmat = max(numSubmat, dp[i][j])
75+
}
76+
}
77+
}
78+
return currentCount
79+
}
80+
}

0 commit comments

Comments
 (0)