forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-with-minimum-effort.cpp
305 lines (284 loc) · 9.91 KB
/
path-with-minimum-effort.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Time: O(m * n * log(m * n))
// Space: O(m * n)
// Dijkstra algorithm solution
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0},
{0, -1}, {-1, 0}};
using T = tuple<int, int, int>;
vector<vector<int>> dist(size(heights), vector<int>(size(heights[0]), numeric_limits<int>::max()));
dist[0][0] = 0;
priority_queue<T, vector<T>, greater<T>> min_heap;
min_heap.emplace(0, 0, 0);
vector<vector<int>> lookup(size(heights), vector<int>(size(heights[0])));
while (!empty(min_heap)) {
const auto [d, r, c] = min_heap.top(); min_heap.pop();
if (lookup[r][c]) {
continue;
}
lookup[r][c] = true;
if (r == size(heights) - 1 && c == size(heights[0]) - 1) {
return d;
}
for (const auto& [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(heights) &&
0 <= nc && nc < size(heights[0]) &&
!lookup[nr][nc])) {
continue;
}
int nd = max(d, abs(heights[nr][nc] - heights[r][c]));
if (nd < dist[nr][nc]) {
dist[nr][nc] = nd;
min_heap.emplace(nd, nr, nc);
}
}
}
return -1;
}
};
// Time: O(m * n * log(m * n) + m * n * α(m * n)) = O(m * n * log(m * n))
// Space: O(m * n)
// union find solution
class Solution2 {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
vector<tuple<int, int, int>> diffs;
for (int i = 0; i < size(heights); ++i) {
for (int j = 0; j < size(heights[0]); ++j) {
if (i > 0) {
diffs.emplace_back(abs(heights[i][j] - heights[i - 1][j]),
index(size(heights[0]), i - 1, j),
index(size(heights[0]), i, j));
}
if (j > 0) {
diffs.emplace_back(abs(heights[i][j] - heights[i][j - 1]),
index(size(heights[0]), i, j - 1),
index(size(heights[0]), i, j));
}
}
}
sort(begin(diffs), end(diffs));
UnionFind union_find(size(heights) * size(heights[0]));
for (const auto& [d, i, j] : diffs) {
if (union_find.union_set(i, j)) {
if (union_find.find_set(index(size(heights[0]), 0, 0)) ==
union_find.find_set(index(size(heights[0]), size(heights) - 1, size(heights[0]) - 1))) {
return d;
}
}
}
return 0;
}
private:
class UnionFind {
public:
UnionFind(const int n)
: set_(n)
, rank_(n)
, count_(n) {
iota(set_.begin(), set_.end(), 0);
}
int find_set(const int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
bool union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root == y_root) {
return false;
}
if (rank_[x_root] < rank_[y_root]) { // Union by rank.
set_[x_root] = y_root;
} else if (rank_[x_root] > rank_[y_root]) {
set_[y_root] = x_root;
} else {
set_[y_root] = x_root;
++rank_[x_root];
}
--count_;
return true;
}
int size() const {
return count_;
}
private:
vector<int> set_;
vector<int> rank_;
int count_;
};
int index(int n, int i, int j) {
return i * n + j;
}
};
// Time: O(m * n * logh)
// Space: O(m * n)
// bi-bfs solution
class Solution3 {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
static const int MAX_H = 1e6;
int left = 0, right = MAX_H;
while (left <= right) {
int mid = left + (right - left) / 2;
if (check(heights, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
private:
bool check(const vector<vector<int>>& heights, int x) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0},
{0, -1}, {-1, 0}};
vector<vector<int>> lookup(size(heights), vector<int>(size(heights[0])));
unordered_set<pair<int, int>, PairHash<int>> left({{0, 0}});
unordered_set<pair<int, int>, PairHash<int>> right({{size(heights) - 1, size(heights[0]) - 1}});
while (!empty(left)) {
for (const auto& [r, c] : left) {
lookup[r][c] = true;
}
unordered_set<pair<int, int>, PairHash<int>> new_left;
for (const auto& [r, c] : left) {
if (right.count(pair(r, c))) {
return true;
}
for (const auto& [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(heights) &&
0 <= nc && nc < size(heights[0]) &&
abs(heights[nr][nc] - heights[r][c]) <= x &&
!lookup[nr][nc])) {
continue;
}
new_left.emplace(nr, nc);
}
}
left = move(new_left);
if (size(left) > size(right)) {
swap(left, right);
}
}
return false;
}
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
};
// Time: O(m * n * logh)
// Space: O(m * n)
// bfs solution
class Solution4 {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
static const int MAX_H = 1e6;
int left = 0, right = MAX_H;
while (left <= right) {
int mid = left + (right - left) / 2;
if (check(heights, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
private:
bool check(const vector<vector<int>>& heights, int x) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0},
{0, -1}, {-1, 0}};
queue<pair<int, int>> q({{0, 0}});
vector<vector<int>> lookup(size(heights), vector<int>(size(heights[0])));
while (!empty(q)) {
const auto [r, c] = q.front(); q.pop();
if (r == size(heights) - 1 && c == size(heights[0]) - 1) {
return true;
}
for (const auto& [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(heights) &&
0 <= nc && nc < size(heights[0]) &&
abs(heights[nr][nc] - heights[r][c]) <= x &&
!lookup[nr][nc])) {
continue;
}
lookup[nr][nc] = true;
q.emplace(nr, nc);
}
}
return false;
}
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
};
// Time: O(m * n * logh)
// Space: O(m * n)
// dfs solution
class Solution5 {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
static const int MAX_H = 1e6;
int left = 0, right = MAX_H;
while (left <= right) {
int mid = left + (right - left) / 2;
if (check(heights, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
private:
bool check(const vector<vector<int>>& heights, int x) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0},
{0, -1}, {-1, 0}};
vector<pair<int, int>> stk({{0, 0}});
vector<vector<int>> lookup(size(heights), vector<int>(size(heights[0])));
while (!empty(stk)) {
const auto [r, c] = stk.back(); stk.pop_back();
if (r == size(heights) - 1 && c == size(heights[0]) - 1) {
return true;
}
for (const auto& [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(heights) &&
0 <= nc && nc < size(heights[0]) &&
abs(heights[nr][nc] - heights[r][c]) <= x &&
!lookup[nr][nc])) {
continue;
}
lookup[nr][nc] = true;
stk.emplace_back(nr, nc);
}
}
return false;
}
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
};