Skip to content
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

Closed
mah-shamim opened this issue Jan 22, 2025 · 1 comment · Fixed by #1208 or #1209
Closed

1765. Map of Highest Peak #1207

mah-shamim opened this issue Jan 22, 2025 · 1 comment · Fixed by #1208 or #1209
Assignees
Labels
medium Difficulty question Further information is requested

Comments

@mah-shamim
Copy link
Owner

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 size m x n that represents a map of land and water cells.

  • If isWater[i][j] == 0, cell (i, j) is a land cell.
  • If 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:

  • The height of each cell must be non-negative.
  • If the cell is a water cell, its height must be 0.
  • Any two adjacent cells must have an absolute height difference of at most 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 size m x n where height[i][j] is cell (i, j)'s height. If there are multiple solutions, return any of them.

Example 1:

screenshot-2021-01-11-at-82045-am

  • Input: isWater = [[0,1],[0,0]]
  • Output: [[1,0],[2,1]]
  • Explanation: The image shows the assigned heights of each cell.
    The blue cell is the water cell, and the green cells are the land cells.

Example 2:

screenshot-2021-01-11-at-82050-am

  • Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
  • Output: [[1,1,0],[0,1,1],[1,2,2]]
  • Explanation: A height of 2 is the maximum possible height of any assignment.
    Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.

Example 3:

  • Input: isWater = [[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,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,2,1,1,0,1,1,0,1,0,1,2,1,2,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,...]]

Constraints:

  • m == isWater.length
  • n == isWater[i].length
  • 1 <= m, n <= 1000
  • isWater[i][j] is 0 or 1.
  • There is at least one water cell.

Hint:

  1. Set each water cell to be 0. The height of each cell is limited by its closest water cell.
  2. Perform a multi-source BFS with all the water cells as sources.

Note: This question is the same as 542. 01 Matrix

@mah-shamim mah-shamim added medium Difficulty question Further information is requested labels Jan 22, 2025
@mah-shamim
Copy link
Owner Author

We can use a breadth-first search (BFS) approach. Here's how we can approach it step-by-step:

Problem Breakdown:

  1. Water Cells: The cells with 1 represent water cells, and their height is always 0.
  2. Land Cells: The cells with 0 represent land cells, and their height should be assigned such that adjacent land cells have a height difference of at most 1.

Approach:

  1. BFS Initialization:

    • We start by marking all the water cells (cells with value 1) as the starting points in the BFS and assign their height to 0.
    • Then we process the neighboring land cells (cells with value 0) to assign heights.
  2. BFS Traversal:

    • From each water cell, we expand outwards, increasing the height by 1 for each adjacent land cell, making sure that the height difference between adjacent cells never exceeds 1.
    • We continue this process until all the cells are visited.
  3. Result: The result will be a matrix of heights that adheres to the given rules, with the height values maximized.

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,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0],[1,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0,1,0,0,1,1,0,1,1,0,0,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,0,1,0,0,1,1,0,0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,1,0,0,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,1,1,0,0,1,0,1,0,0,0,0,1,0,1,0,1,1,0,0,0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,1,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,1,0,1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0,0,1,0,1,1,2,3,2,2,2,2,2,2,3,2,3,3,2,1,0,1,2,1,1,2,1,0,1,0,0,0,1,1,0,1,2,3,2,1,0,1,2,1,1,0,1,1,0,1,2],[0,0,1,1,2,2,1,0,1,1,1,0,1,2,1,0,0,1,1,0,1,1,0,0,1,0,0,1,1,0,0,1,1,1,0,1,1,1,1,0,1,1,2,2,1,0,0,1,1,1,0,1,0,1,1,0,0,1,2,1,0,1,2,1,0,0,1,0,1,0,1,2,1,0,1,1,0,0,0,0,1,2,3,2,1,1,0,1,1,1,1,0,1,0,1,0,0,1,1,0,1,0,1,1,1,1,0,1,0,1,0,0,1,1,1,0,0,0,0,1,1,1,0,1,0,1,2,1,1,0,0,0,1,0,1,2,2,1,1,0,1,0,1,1,0,1,1,1,1,0,1,0,0,1,1,1,0,0,0,1,2,1,0,0,1,1,0,1,0,1,2,1,1,0,1,2,1,1,1,1,1,1,2,1,2,3,3,2,1,0,1,0,0,1,0,1,0,0,1,0,1,2,1,2,3,2,1,1,0,1,1,0,1,0,1,2,1,2,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:

  1. Initialization:

    • We initialize the height matrix with -1 for all cells. The water cells are immediately set to 0.
    • The water cells are enqueued to the BFS queue.
  2. BFS:

    • We process the queue by dequeuing each cell, and for each of its neighboring cells, we check if it is within bounds and unvisited.
    • If it's a valid land cell (unvisited), we assign it a height that is one more than the current cell's height and enqueue it for further processing.
  3. Result:

    • After BFS completes, the height matrix will contain the highest possible heights for each cell, respecting the given constraints.

Time Complexity:

  • O(m * n) where m is the number of rows and n is the number of columns. This is because each cell is processed at most once during the BFS traversal.

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.

mah-shamim added a commit that referenced this issue Jan 22, 2025
… 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]>
This was linked to pull requests Jan 22, 2025
mah-shamim added a commit that referenced this issue Jan 22, 2025
… 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
medium Difficulty question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant