代码拉取完成,页面将自动刷新
// Time: O(n^2)
// Space: O(n^2)
class Solution {
public:
int swimInWater(vector<vector<int>>& grid) {
const int n = grid.size();
vector<pair<int, int>> positions(n * n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
positions[grid[i][j]] = {i, j};
}
}
static const vector<pair<int, int>> directions{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
UnionFind union_find(n * n);
for (int elevation = 0; elevation < positions.size(); ++elevation) {
int i, j;
tie(i, j) = positions[elevation];
for (const auto& dir : directions) {
int x = i + dir.first;
int y = j + dir.second;
if (0 <= x && x < n &&
0 <= y && y < n &&
grid[x][y] <= elevation) {
union_find.union_set(i * n + j, x * n + y);
if (union_find.find_set(0) == union_find.find_set(n * n - 1)) {
return elevation;
}
}
}
}
return n * n - 1;
}
private:
class UnionFind {
public:
UnionFind(const int n) : set_(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];
}
void union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root != y_root) {
set_[min(x_root, y_root)] = max(x_root, y_root);
}
}
private:
vector<int> set_;
};
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。