-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1631-path-with-minimum-effort.kt
141 lines (116 loc) · 3.69 KB
/
1631-path-with-minimum-effort.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//dijkstra
class Solution {
fun minimumEffortPath(h: Array<IntArray>): Int {
val minHeap = PriorityQueue<IntArray> { a, b -> a[2] - b[2] }
val dirs = intArrayOf(0, 1, 0, -1, 0)
val n = h.size
val m = h[0].size
val visited = Array (n) { BooleanArray (m) }
fun isValid(x: Int, y: Int) = x in (0 until n) && y in (0 until m)
minHeap.add(intArrayOf(0, 0, 0))
while (minHeap.isNotEmpty()) {
val (i, j, e) = minHeap.poll()
if (i == n - 1 && j == m - 1) return e
visited[i][j] = true
for (k in 0..3) {
val i2 = i + dirs[k]
val j2 = j + dirs[k + 1]
if (isValid(i2, j2) && !visited[i2][j2]) {
val e2 = Math.abs(h[i][j] - h[i2][j2])
minHeap.add(intArrayOf(i2, j2, maxOf(e, e2)))
}
}
}
return 0
}
}
// binary search + dfs to find min effort to reach end from start
class Solution {
fun minimumEffortPath(h: Array<IntArray>): Int {
val dirs = intArrayOf(0, 1, 0, -1, 0)
val n = h.size
val m = h[0].size
var visited = Array (n) { BooleanArray (m) }
fun isValid(x: Int, y: Int) = x in (0 until n) && y in (0 until m)
fun dfs(x: Int, y: Int, k: Int): Boolean {
if (x == n - 1 && y == m - 1) return true
visited[x][y] = true
for (i in 0..3) {
val x2 = x + dirs[i]
val y2 = y + dirs[i + 1]
if (isValid(x2, y2) && !visited[x2][y2] && Math.abs(h[x][y] - h[x2][y2]) <= k) {
if (dfs(x2, y2, k))
return true
}
}
return false
}
var left = 0
var right = 1000000
var res = right
while (left <= right) {
val mid = (right + left) / 2
visited = Array (n) { BooleanArray (m) }
if (dfs(0, 0, mid)) {
res = mid
right = mid - 1
} else {
left = mid + 1
}
}
return res
}
}
//MST with kruskals algorith (using DSU)
class Solution {
fun minimumEffortPath(h: Array<IntArray>): Int {
val n = h.size
val m = h[0].size
val dsu = DSU(n * m)
val edges = mutableListOf<IntArray>()
fun c(x: Int, y: Int) = x * m + y
for (i in 0 until n) {
for (j in 0 until m) {
if (i + 1 < n) {
val e = Math.abs(h[i][j] - h[i + 1][j])
edges.add(intArrayOf(c(i, j), c(i + 1, j), e))
}
if (j + 1 < m) {
val e = Math.abs(h[i][j] - h[i][j + 1])
edges.add(intArrayOf(c(i, j), c(i, j + 1), e))
}
}
}
edges.sortWith { a, b -> a[2] - b[2] }
for ((u, v, e) in edges) {
if (dsu.union(u, v)) {
if (dsu.find(c(0, 0)) == dsu.find(c(n - 1, m - 1))) {
return e
}
}
}
return 0
}
}
class DSU(val n: Int) {
val parent = IntArray (n) { it }
val size = IntArray (n) { 1 }
fun find(x: Int): Int {
if (parent[x] != x)
parent[x] = find(parent[x])
return parent[x]
}
fun union(x: Int, y: Int): Boolean {
val p1 = find(x)
val p2 = find(y)
if (p1 == p2) return false
if (size[p1] > size[p2]) {
parent[p2] = p1
size[p1] += size[p2]
} else {
parent[p1] = p2
size[p2] += size[p1]
}
return true
}
}