Skip to content

Commit 23c3da6

Browse files
committed
Sync LeetCode submission Runtime - 1038 ms (52.23%), Memory - 61.1 MB (90.35%)
1 parent 14e79f3 commit 23c3da6

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

1876-map-of-highest-peak/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>You are given an integer matrix <code>isWater</code> of size <code>m x n</code> that represents a map of <strong>land</strong> and <strong>water</strong> cells.</p>
2+
3+
<ul>
4+
<li>If <code>isWater[i][j] == 0</code>, cell <code>(i, j)</code> is a <strong>land</strong> cell.</li>
5+
<li>If <code>isWater[i][j] == 1</code>, cell <code>(i, j)</code> is a <strong>water</strong> cell.</li>
6+
</ul>
7+
8+
<p>You must assign each cell a height in a way that follows these rules:</p>
9+
10+
<ul>
11+
<li>The height of each cell must be non-negative.</li>
12+
<li>If the cell is a <strong>water</strong> cell, its height must be <code>0</code>.</li>
13+
<li>Any two adjacent cells must have an absolute height difference of <strong>at most</strong> <code>1</code>. 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).</li>
14+
</ul>
15+
16+
<p>Find an assignment of heights such that the maximum height in the matrix is <strong>maximized</strong>.</p>
17+
18+
<p>Return <em>an integer matrix </em><code>height</code><em> of size </em><code>m x n</code><em> where </em><code>height[i][j]</code><em> is cell </em><code>(i, j)</code><em>&#39;s height. If there are multiple solutions, return <strong>any</strong> of them</em>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82045-am.png" style="width: 220px; height: 219px;" /></strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> isWater = [[0,1],[0,0]]
27+
<strong>Output:</strong> [[1,0],[2,1]]
28+
<strong>Explanation:</strong> The image shows the assigned heights of each cell.
29+
The blue cell is the water cell, and the green cells are the land cells.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-82050-am.png" style="width: 300px; height: 296px;" /></strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> isWater = [[0,0,1],[1,0,0],[0,0,0]]
38+
<strong>Output:</strong> [[1,1,0],[0,1,1],[1,2,2]]
39+
<strong>Explanation:</strong> A height of 2 is the maximum possible height of any assignment.
40+
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>m == isWater.length</code></li>
48+
<li><code>n == isWater[i].length</code></li>
49+
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
50+
<li><code>isWater[i][j]</code> is <code>0</code> or <code>1</code>.</li>
51+
<li>There is at least <strong>one</strong> water cell.</li>
52+
</ul>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Note:</strong> This question is the same as 542: <a href="https://leetcode.com/problems/01-matrix/description/" target="_blank">https://leetcode.com/problems/01-matrix/</a></p>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Approach 1: Breadth First Search
2+
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(m * n)
5+
# Space: O(m * n)
6+
7+
from collections import deque
8+
9+
class Solution:
10+
def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
11+
dx = [0, 0, 1, -1]
12+
dy = [1, -1, 0, 0]
13+
14+
rows, cols = len(isWater), len(isWater[0])
15+
16+
cell_heights = [[-1 for _ in range(cols)] for _ in range(rows)]
17+
18+
# Queue for BFS
19+
cell_queue = deque()
20+
21+
# Add all water cells to the queue and set their heights to 0
22+
for x in range(rows):
23+
for y in range(cols):
24+
if isWater[x][y] == 1:
25+
cell_queue.append((x, y))
26+
cell_heights[x][y] = 0
27+
28+
height_of_next_layer = 1
29+
30+
while cell_queue:
31+
layer_size = len(cell_queue)
32+
33+
# Iterate all cells in the current layer
34+
for _ in range(layer_size):
35+
current_cell = cell_queue.popleft()
36+
37+
for d in range(4):
38+
neighbor_x = current_cell[0] + dx[d]
39+
neighbor_y = current_cell[1] + dy[d]
40+
41+
if self._is_valid_cell(neighbor_x, neighbor_y, rows, cols) and cell_heights[neighbor_x][neighbor_y] == -1:
42+
43+
cell_heights[neighbor_x][neighbor_y] = height_of_next_layer
44+
cell_queue.append((neighbor_x, neighbor_y))
45+
46+
height_of_next_layer += 1
47+
48+
return cell_heights
49+
50+
def _is_valid_cell(self, x, y, rows, cols):
51+
return 0 <= x < rows and 0 <= y < cols

0 commit comments

Comments
 (0)