-
Notifications
You must be signed in to change notification settings - Fork 0
/
1765.heightestPeak.cpp
31 lines (31 loc) · 1.09 KB
/
1765.heightestPeak.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
class Solution {
public:
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
int n = isWater.size(), m = isWater[0].size();
vector<vector<int>> res(n, vector<int>(m, -1));
queue<pair<int, int>> q;
for(int i = 0; i < isWater.size(); i++){
for(int j = 0; j < isWater[0].size(); j++){
if(isWater[i][j]){
q.push({i ,j});
res[i][j] = 0;
}
}
}
vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
while(!q.empty()){
pair<int, int> node = q.front();
q.pop();
for(auto d : dir){
int x = node.first + d.first;
int y = node.second + d.second;
if( x >= 0 && y >= 0 && x < isWater.size() && y < isWater[0].size() && res[x][y] == -1)
{
res[x][y] = res[node.first][node.second] + 1;
q.push({x, y});
}
}
}
return res;
}
};