Skip to content

Commit bba262c

Browse files
committed
1631. Path With Minimum Effort
1 parent d6fc8f1 commit bba262c

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,9 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
616616
|47| [1245. Tree Diameter](https://tinyurl.com/yxq4uykv) | [Python](https://tinyurl.com/wu6rdaw/1245_Tree_Diameter.py), [Swift](https://tinyurl.com/wuja3c4/1245_Tree_Diameter.swift) | [Art 1](https://www.geeksforgeeks.org/longest-path-undirected-tree/) | Medium |📌 Learned, 2 BFS. Important and interesting |
617617
|48| [1376. Time Needed to Inform All Employees](https://tinyurl.com/y3e5bb22) | [Python](https://tinyurl.com/wu6rdaw/1376_Time_Needed_to_Inform_All_Employees.py), [Swift](https://tinyurl.com/wuja3c4/1376_Time_Needed_to_Inform_All_Employees.swift) | [Art 1](https://tinyurl.com/y2lw6s4j) | Medium |📌 BFS, DFS |
618618
|49| [286. Walls and Gates](https://tinyurl.com/y3ollzsl) | [Python](https://tinyurl.com/wu6rdaw/286_Walls_and_Gates.py), [Swift](https://tinyurl.com/wuja3c4/286_Walls_and_Gates.swift) | --- | Medium |📌 BFS, DFS |
619-
|50| [286. Walls and Gates](https://tinyurl.com/y2xk6r98) | [Python](https://tinyurl.com/wu6rdaw/1138_Alphabet_Board_Path.py), [Swift](https://tinyurl.com/wuja3c4/1138_Alphabet_Board_Path.swift) | --- | Medium |📌 BFS |
619+
|50| [1138. Alphabet Board Path](https://tinyurl.com/y2xk6r98) | [Python](https://tinyurl.com/wu6rdaw/1138_Alphabet_Board_Path.py), [Swift](https://tinyurl.com/wuja3c4/1138_Alphabet_Board_Path.swift) | --- | Medium |📌 BFS |
620620
|51| [690. Employee Importance](https://tinyurl.com/yyweuk43) | [Python](https://tinyurl.com/wu6rdaw/690_Employee_Importance.py), [Swift](https://tinyurl.com/wuja3c4/690_Employee_Importance.swift) | --- | Easy |📌 BFS |
621+
|52| **[1631. Path With Minimum Effort](https://tinyurl.com/y3nh25d9) **| [Python](https://tinyurl.com/wu6rdaw/1631_Path_With_Minimum_Effort.py), [Swift](https://tinyurl.com/wuja3c4/1631_Path_With_Minimum_Effort.swift) | --- | Medium |📌 BFS, DFS, Binary search |
621622

622623

623624
</p>

leetcode.com/swift/DS_ALGO_PRAC.playground/Contents.swift

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,42 @@ class Solution {
44
guard !heights.isEmpty, !heights[0].isEmpty else {
55
return 0
66
}
7-
var heights = heights
8-
var minEffort = heights[0][0]
9-
var queue = Array([0,0])
10-
while !queue.isEmpty {
11-
var currentCell = queue.removeFirst()
12-
var (currentX, currentY) = (currentCell[0], currentCell[1])
13-
var currentMinEffort = heights[currentX][currentY]
14-
var currentLevelSize = queue.count
15-
16-
for _ in 0..<currentLevelSize {
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]
1729

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

0 commit comments

Comments
 (0)