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

001765 map of highest peak #1208

Merged
merged 101 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
e35dee5
Automated commit 1
mah-shamim Jan 22, 2025
2b64379
Automated commit rollback 1
mah-shamim Jan 22, 2025
033d523
Automated commit 2
mah-shamim Jan 22, 2025
562a585
Automated commit rollback 2
mah-shamim Jan 22, 2025
4b8fb97
Automated commit 3
mah-shamim Jan 22, 2025
ac11063
Automated commit rollback 3
mah-shamim Jan 22, 2025
060514d
Automated commit 4
mah-shamim Jan 22, 2025
ff6676c
Automated commit rollback 4
mah-shamim Jan 22, 2025
15ace8b
Automated commit 5
mah-shamim Jan 22, 2025
3532ae7
Automated commit rollback 5
mah-shamim Jan 22, 2025
d2b13c8
Automated commit 6
mah-shamim Jan 22, 2025
b54ac4a
Automated commit rollback 6
mah-shamim Jan 22, 2025
d73d019
Automated commit 7
mah-shamim Jan 22, 2025
b316119
Automated commit rollback 7
mah-shamim Jan 22, 2025
163c292
Automated commit 8
mah-shamim Jan 22, 2025
9d6e764
Automated commit rollback 8
mah-shamim Jan 22, 2025
b89a7d3
Automated commit 9
mah-shamim Jan 22, 2025
9b26c32
Automated commit rollback 9
mah-shamim Jan 22, 2025
016969c
Automated commit 10
mah-shamim Jan 22, 2025
9ea286b
Automated commit rollback 10
mah-shamim Jan 22, 2025
d5bac11
Automated commit 11
mah-shamim Jan 22, 2025
f82bcd5
Automated commit rollback 11
mah-shamim Jan 22, 2025
9f32247
Automated commit 12
mah-shamim Jan 22, 2025
47d99bd
Automated commit rollback 12
mah-shamim Jan 22, 2025
54d4a73
Automated commit 13
mah-shamim Jan 22, 2025
19cd2c8
Automated commit rollback 13
mah-shamim Jan 22, 2025
3dbc4ad
Automated commit 14
mah-shamim Jan 22, 2025
3a67c8e
Automated commit rollback 14
mah-shamim Jan 22, 2025
2effd4b
Automated commit 15
mah-shamim Jan 22, 2025
abc3054
Automated commit rollback 15
mah-shamim Jan 22, 2025
50ec25d
Automated commit 16
mah-shamim Jan 22, 2025
8715387
Automated commit rollback 16
mah-shamim Jan 22, 2025
81d9aa8
Automated commit 17
mah-shamim Jan 22, 2025
16df679
Automated commit rollback 17
mah-shamim Jan 22, 2025
b6a761b
Automated commit 18
mah-shamim Jan 22, 2025
27eae2a
Automated commit rollback 18
mah-shamim Jan 22, 2025
6b86de7
Automated commit 19
mah-shamim Jan 22, 2025
96e2b2a
Automated commit rollback 19
mah-shamim Jan 22, 2025
4603537
Automated commit 20
mah-shamim Jan 22, 2025
9568e27
Automated commit rollback 20
mah-shamim Jan 22, 2025
5849bcf
Automated commit 21
mah-shamim Jan 22, 2025
97c1f47
Automated commit rollback 21
mah-shamim Jan 22, 2025
b3c827c
Automated commit 22
mah-shamim Jan 22, 2025
d24bfef
Automated commit rollback 22
mah-shamim Jan 22, 2025
3763638
Automated commit 23
mah-shamim Jan 22, 2025
ea941f0
Automated commit rollback 23
mah-shamim Jan 22, 2025
73ca3e2
Automated commit 24
mah-shamim Jan 22, 2025
2d981f1
Automated commit rollback 24
mah-shamim Jan 22, 2025
1031c36
Automated commit 25
mah-shamim Jan 22, 2025
6026416
Automated commit rollback 25
mah-shamim Jan 22, 2025
ef7f504
Automated commit 26
mah-shamim Jan 22, 2025
01fa717
Automated commit rollback 26
mah-shamim Jan 22, 2025
2b9ea3f
Automated commit 27
mah-shamim Jan 22, 2025
c44e3ec
Automated commit rollback 27
mah-shamim Jan 22, 2025
9295dd7
Automated commit 28
mah-shamim Jan 22, 2025
ef7f170
Automated commit rollback 28
mah-shamim Jan 22, 2025
b7fbb52
Automated commit 29
mah-shamim Jan 22, 2025
dfd0cc5
Automated commit rollback 29
mah-shamim Jan 22, 2025
9208db8
Automated commit 30
mah-shamim Jan 22, 2025
ab39405
Automated commit rollback 30
mah-shamim Jan 22, 2025
d1148e2
Automated commit 31
mah-shamim Jan 22, 2025
f105fe6
Automated commit rollback 31
mah-shamim Jan 22, 2025
f905e4f
Automated commit 32
mah-shamim Jan 22, 2025
fbe12ec
Automated commit rollback 32
mah-shamim Jan 22, 2025
13d3f8a
Automated commit 33
mah-shamim Jan 22, 2025
aefc9a6
Automated commit rollback 33
mah-shamim Jan 22, 2025
be58232
Automated commit 34
mah-shamim Jan 22, 2025
09e944f
Automated commit rollback 34
mah-shamim Jan 22, 2025
95e8cc2
Automated commit 35
mah-shamim Jan 22, 2025
bf30b81
Automated commit rollback 35
mah-shamim Jan 22, 2025
72506a4
Automated commit 36
mah-shamim Jan 22, 2025
889aec8
Automated commit rollback 36
mah-shamim Jan 22, 2025
a2250b2
Automated commit 37
mah-shamim Jan 22, 2025
e301697
Automated commit rollback 37
mah-shamim Jan 22, 2025
e2c77af
Automated commit 38
mah-shamim Jan 22, 2025
1eb005d
Automated commit rollback 38
mah-shamim Jan 22, 2025
bb62936
Automated commit 39
mah-shamim Jan 22, 2025
87051dd
Automated commit rollback 39
mah-shamim Jan 22, 2025
2ad7a86
Automated commit 40
mah-shamim Jan 22, 2025
587475a
Automated commit rollback 40
mah-shamim Jan 22, 2025
3ae5134
Automated commit 41
mah-shamim Jan 22, 2025
fcb98d4
Automated commit rollback 41
mah-shamim Jan 22, 2025
15c7dbb
Automated commit 42
mah-shamim Jan 22, 2025
e8335a7
Automated commit rollback 42
mah-shamim Jan 22, 2025
a385e62
Automated commit 43
mah-shamim Jan 22, 2025
eadadfc
Automated commit rollback 43
mah-shamim Jan 22, 2025
e4dedb8
Automated commit 44
mah-shamim Jan 22, 2025
d6ea041
Automated commit rollback 44
mah-shamim Jan 22, 2025
cd40e46
Automated commit 45
mah-shamim Jan 22, 2025
ab9777b
Automated commit rollback 45
mah-shamim Jan 22, 2025
52a8b18
Automated commit 46
mah-shamim Jan 22, 2025
5aba242
Automated commit rollback 46
mah-shamim Jan 22, 2025
6c27eaa
Automated commit 47
mah-shamim Jan 22, 2025
3ae9956
Automated commit rollback 47
mah-shamim Jan 22, 2025
6382177
Automated commit 48
mah-shamim Jan 22, 2025
cc174ff
Automated commit rollback 48
mah-shamim Jan 22, 2025
53fa787
Automated commit 49
mah-shamim Jan 22, 2025
fc45a89
Automated commit rollback 49
mah-shamim Jan 22, 2025
97cf8e4
Automated commit 50
mah-shamim Jan 22, 2025
3d8c33a
Automated commit rollback 50
mah-shamim Jan 22, 2025
a118de3
#1206, #1207 leetcode problems 001765-map-of-highest-peak submissions…
mah-shamim Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions algorithms/001765-map-of-highest-peak/README.md
mah-shamim marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
1765\. Map of Highest Peak

**Difficulty:** Medium

**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](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82045-am.png)

- **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](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82050-am.png)

- **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,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,1,0,0,0,1,1,1,0,0,0,1,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,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,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,...]]


**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](https://leetcode.com/problems/01-matrix/)



**Solution:**

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](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001765-map-of-highest-peak/solution.php)**

```php
<?php
/**
* @param Integer[][] $isWater
* @return Integer[][]
*/
function highestPeak($isWater) {
...
...
...
/**
* go to ./solution.php
*/
}

// 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,1,0,0,0,1,1,1,0,0,0,1,1,0,0,1,0,1,0,0,0,0,1,0,0,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,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.

**Contact Links**

If you found this series helpful, please consider giving the **[repository](https://github.com/mah-shamim/leet-code-in-php)** a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

- **[LinkedIn](https://www.linkedin.com/in/arifulhaque/)**
- **[GitHub](https://github.com/mah-shamim)**
4 changes: 4 additions & 0 deletions algorithms/001765-map-of-highest-peak/example-03.md
mah-shamim marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions algorithms/001765-map-of-highest-peak/solution.php
mah-shamim marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class Solution {

/**
* @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;
}
}