File tree 1 file changed +8
-7
lines changed
scripts/algorithms/M/Map of Highest Peak
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 344 ms (Top 99.56%) | Memory: 108.2 MB (Top 83.84%)
1
2
class Solution {
2
3
public:
3
4
4
5
vector<vector<int >> highestPeak (vector<vector<int >>& isWater) {
5
6
int r = isWater.size ();
6
7
int c = isWater[0 ].size ();
7
8
queue <pair<int ,int >> curr;
8
-
9
+
9
10
for (int i = 0 ; i < r; i++) {
10
11
for (int j = 0 ; j < c; j++) {
11
12
// set the land cell to -1 (not visited)
@@ -19,18 +20,18 @@ class Solution {
19
20
}
20
21
}
21
22
}
22
-
23
+
23
24
int hill = 0 ;
24
25
while (!curr.empty ()) {
25
26
int len = curr.size ();
26
-
27
+
27
28
for (int k = 0 ; k < len; k++) {
28
-
29
+
29
30
// for each cell check its 4 boundary cells
30
31
// if it is not visited, increase its hill by 1
31
32
pair <int , int > fnt = curr.front (); curr.pop ();
32
33
int i = fnt.first , j = fnt.second ;
33
-
34
+
34
35
// top cell
35
36
if (i > 0 && isWater[i - 1 ][j] == -1 ) {
36
37
isWater[i - 1 ][j] = hill + 1 ;
@@ -52,11 +53,11 @@ class Solution {
52
53
curr.push ({i, j+1 });
53
54
}
54
55
}
55
-
56
+
56
57
// after 1 complete round increase the height of the hill
57
58
hill += 1 ;
58
59
}
59
-
60
+
60
61
return isWater;
61
62
}
62
63
};
You can’t perform that action at this time.
0 commit comments