leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1765.cpp (1100B)
0 class Solution {
1 typedef tuple<int, int> record;
3 public:
4 vector<vector<int>> highestPeak(vector<vector<int>> &isWater) {
5 static const int offset[] = {-1, 0, 1, 0, -1};
6 const int n = isWater.size(), m = isWater[0].size();
8 queue<record> q;
9 for (int i = 0; i < n; i++) {
10 for (int j = 0; j < m; j++) {
11 if (isWater[i][j])
12 isWater[i][j] = 0, q.push({i, j});
13 else
14 isWater[i][j] = INT_MAX;
15 }
16 }
18 while (!q.empty()) {
19 for (int k = q.size(); k > 0; k--) {
20 const auto [i, j] = q.front();
21 q.pop();
22 for (int k = 0; k < 4; k++) {
23 const int x = i + offset[k], y = j + offset[k + 1];
24 if (x < 0 || x >= n || y < 0 || y >= m) continue;
25 if (isWater[x][y] != INT_MAX) continue;
26 isWater[x][y] = isWater[i][j] + 1;
27 q.push({x, y});
28 }
29 }
30 }
32 return isWater;
33 }
34 };