Skip to content

Commit 8e019b3

Browse files
authored
Create 1765. Map of Highest Peak (#694)
2 parents 5e0809d + 205a6c1 commit 8e019b3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

1765. Map of Highest Peak

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
void bfs(vector<vector<int>> &visited,vector<vector<int>> &isWater,vector<vector<int>> &ans,queue<pair<int,int>> &q){
3+
// visited[i][j]=1;
4+
int n=isWater.size(),m=isWater[0].size();
5+
vector<int> newx={0,1,0,-1};
6+
vector<int> newy={-1,0,1,0};
7+
// q.push({i,j});
8+
while(!q.empty()){
9+
int x=q.front().first;
10+
int y=q.front().second;
11+
q.pop();
12+
for(int k=0;k<4;k++){
13+
int new_x=x+newx[k];
14+
int new_y=y+newy[k];
15+
if(new_x>=0 and new_x<n and new_y>=0 and new_y<m and ans[new_x][new_y]==-1){
16+
ans[new_x][new_y]=1+ans[x][y];
17+
visited[new_x][new_y]=1;
18+
q.push({new_x,new_y});
19+
}
20+
}
21+
}
22+
}
23+
public:
24+
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
25+
int n=isWater.size(),m=isWater[0].size();
26+
vector<vector<int>> visited(n,vector<int> (m,0));
27+
vector<vector<int>> ans(n,vector<int> (m,-1));
28+
queue<pair<int,int>> q;
29+
for(int i=0;i<n;i++){
30+
for(int j=0;j<m;j++){
31+
if(isWater[i][j]==1){
32+
ans[i][j]=0;
33+
q.push({i,j});
34+
}
35+
}
36+
}
37+
bfs(visited,isWater,ans,q);
38+
return ans;
39+
}
40+
};

0 commit comments

Comments
 (0)