|
| 1 | +# 1162. As Far from Land as Possible |
| 2 | +# 🟠 Medium |
| 3 | +# |
| 4 | +# https://leetcode.com/problems/as-far-from-land-as-possible/ |
| 5 | +# |
| 6 | +# Tags: Array - Dynamic Programming - Breadth-First Search - Matrix |
| 7 | + |
| 8 | +import timeit |
| 9 | +from typing import List |
| 10 | + |
| 11 | + |
| 12 | +# Use BFS and a matrix with the current shortest distance between any |
| 13 | +# cell and land to compute the shortest distance between any cell and |
| 14 | +# land, we will only revisit cells when the new distance to land is |
| 15 | +# less than the shortest previous distance found, which may, in turn, |
| 16 | +# influence its neighbors by providing a shorter path to land. |
| 17 | +# |
| 18 | +# Time complexity: O(m+n) - We can travel a maximum of n levels, on each |
| 19 | +# level, we will only visit cells to which we have not previously found |
| 20 | +# a shorter route, in total we will visit m*n cells during the BFS. |
| 21 | +# Space complexity: O(m+n) - The distance matrix has the same size as |
| 22 | +# the input matrix, m rows and n columns. |
| 23 | +# |
| 24 | +# Runtime 548 ms Beats 82.97% |
| 25 | +# Memory 14.6 MB Beats 64.75% |
| 26 | +class Solution: |
| 27 | + def maxDistance(self, grid: List[List[int]]) -> int: |
| 28 | + m, n = len(grid), len(grid[0]) |
| 29 | + # A matrix of distances to land. |
| 30 | + dist = [[float("inf")] * n for _ in range(m)] |
| 31 | + # Use a stack to do a per-level BFS. |
| 32 | + current_level, level = [], 0 |
| 33 | + # Add land cells to the first level. |
| 34 | + for i in range(m): |
| 35 | + for j in range(n): |
| 36 | + if grid[i][j] == 1: |
| 37 | + current_level.append((i, j)) |
| 38 | + # Mark this cell as 0 units away from land. |
| 39 | + dist[i][j] = 0 |
| 40 | + # If the grid has no water or land, return -1. |
| 41 | + if not len(current_level) or len(current_level) == (m * n): |
| 42 | + return -1 |
| 43 | + # Keep exploring as long as the current level has any elements. |
| 44 | + while current_level: |
| 45 | + next_level = [] |
| 46 | + level += 1 |
| 47 | + # Process one level at a time. |
| 48 | + for _ in range(len(current_level)): |
| 49 | + r, c = current_level.pop() |
| 50 | + for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)): |
| 51 | + # The cell needs to be within boundaries, it should |
| 52 | + # not be land and we should not have a shorter path |
| 53 | + # to land already. |
| 54 | + if 0 <= nr < m and 0 <= nc < n and level < dist[nr][nc]: |
| 55 | + dist[nr][nc] = level |
| 56 | + next_level.append((nr, nc)) |
| 57 | + current_level = next_level |
| 58 | + return level - 1 |
| 59 | + |
| 60 | + |
| 61 | +def test(): |
| 62 | + executors = [Solution] |
| 63 | + tests = [ |
| 64 | + [[[1]], -1], |
| 65 | + [[[0]], -1], |
| 66 | + [[[0, 0], [0, 0], [0, 0]], -1], |
| 67 | + [[[1, 1], [1, 1], [1, 1]], -1], |
| 68 | + [[[1, 0], [0, 0], [1, 0]], 2], |
| 69 | + [[[1, 0, 1], [0, 0, 0], [1, 0, 1]], 2], |
| 70 | + [[[1, 0, 0], [0, 0, 0], [0, 0, 0]], 4], |
| 71 | + [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], -1], |
| 72 | + ] |
| 73 | + for executor in executors: |
| 74 | + start = timeit.default_timer() |
| 75 | + for _ in range(1): |
| 76 | + for col, t in enumerate(tests): |
| 77 | + sol = executor() |
| 78 | + result = sol.maxDistance(t[0]) |
| 79 | + exp = t[1] |
| 80 | + assert result == exp, ( |
| 81 | + f"\033[93m» {result} <> {exp}\033[91m for" |
| 82 | + + f" test {col} using \033[1m{executor.__name__}" |
| 83 | + ) |
| 84 | + stop = timeit.default_timer() |
| 85 | + used = str(round(stop - start, 5)) |
| 86 | + cols = "{0:20}{1:10}{2:10}" |
| 87 | + res = cols.format(executor.__name__, used, "seconds") |
| 88 | + print(f"\033[92m» {res}\033[0m") |
| 89 | + |
| 90 | + |
| 91 | +test() |
0 commit comments