-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1765. Map of Highest Peak #1207
Comments
We can use a breadth-first search (BFS) approach. Here's how we can approach it step-by-step: Problem Breakdown:
Approach:
Let's implement this solution in PHP: 1765. Map of Highest Peak <?php
/**
* @param Integer[][] $isWater
* @return Integer[][]
*/
function highestPeak($isWater) {
$dirs = [
[0, 1], [1, 0], [0, -1], [-1, 0]
];
$m = count($isWater);
$n = count($isWater[0]);
$ans = array_fill(0, $m, array_fill(0, $n, -1));
$q = new SplQueue();
// Initialize the queue with water cells and mark their height as 0
for ($i = 0; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
if ($isWater[$i][$j] == 1) {
$q->enqueue([$i, $j]);
$ans[$i][$j] = 0;
}
}
}
// BFS to find the highest peak
while (!$q->isEmpty()) {
list($i, $j) = $q->dequeue();
foreach ($dirs as $dir) {
$dx = $dir[0];
$dy = $dir[1];
$x = $i + $dx;
$y = $j + $dy;
if ($x < 0 || $x == $m || $y < 0 || $y == $n) continue;
if ($ans[$x][$y] != -1) continue;
$ans[$x][$y] = $ans[$i][$j] + 1;
$q->enqueue([$x, $y]);
}
}
return $ans;
}
// Example usage:
$$isWater1 = [[0,1],[0,0]];
$$isWater2 = [[0,0,1],[1,0,0],[0,0,0]];
$$isWater3 = [[1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,1,1,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,0,1,0,0,0,0,1,0,1,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,3],[1,1,0,0,1,1,0,1,1,2,1,0,1,1,1,0,1,0,1,0,1,1,0,1,2,1,1,0,1,1,1,1,0,1,1,2,1,0,1,1,2,1,2,2,1,1,0,1,0,1,0,1,1,2,1,0,1,2,1,...]]
?> Explanation:
Time Complexity:
This solution ensures that the matrix is filled with the correct heights, and the BFS guarantees the maximum height for each cell while maintaining the height difference constraint between adjacent cells. |
… 1517070343 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
… 1517070343 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #1206
Originally posted by mah-shamim January 22, 2025
Topics:
Array
,Breadth-First Search
,Matrix
You are given an integer matrix
isWater
of sizem x n
that represents a map of land and water cells.isWater[i][j] == 0
, cell(i, j)
is a land cell.isWater[i][j] == 1
, cell(i, j)
is a water cell.You must assign each cell a height in a way that follows these rules:
0
.1
. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).Find an assignment of heights such that the maximum height in the matrix is maximized.
Return an integer matrix
height
of sizem x n
whereheight[i][j]
is cell(i, j)
's height. If there are multiple solutions, return any of them.Example 1:
The blue cell is the water cell, and the green cells are the land cells.
Example 2:
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
Example 3:
Constraints:
m == isWater.length
n == isWater[i].length
1 <= m, n <= 1000
isWater[i][j]
is0
or1
.Hint:
Note: This question is the same as 542. 01 Matrix
The text was updated successfully, but these errors were encountered: