As Far from Land as Possible - Complete Solution Guide
As Far from Land as Possible is LeetCode problem 1162, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an n x n grid containing only values 0 and 1 , where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1 . The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1| . Example 1: Input: grid = [[1,0,1],[0,0,0],[1,0,1]] Output: 2 Explanation: The cell (1, 1) is as far a
Detailed Explanation
The problem asks us to find the maximum Manhattan distance from a water cell (value 0) to the nearest land cell (value 1) in a given n x n grid. The Manhattan distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|. If the grid contains no land or no water, we should return -1.
Solution Approach
The solution uses a multi-source Breadth-First Search (BFS) to find the maximum distance from a water cell to the nearest land cell. First, we enqueue all land cells into the queue. Then, we perform a standard BFS. Each layer of the BFS represents an increment in distance. We explore the grid layer by layer, marking visited water cells and adding them to the queue for the next level. We keep track of the distance and update it after each layer. The algorithm stops when the queue is empty, which means all reachable water cells have been visited. The final distance is the maximum distance from any water cell to its nearest land cell.
Step-by-Step Algorithm
- Step 1: Initialize a queue and enqueue all land cells (cells with value 1) from the grid.
- Step 2: Handle edge cases: If the queue is empty (no land) or the queue contains all the cells in the grid (no water), return -1.
- Step 3: Initialize the distance to -1. This will be incremented at the start of each BFS level.
- Step 4: Perform a BFS: While the queue is not empty:
- Step 5: Increment the distance by 1 (start of a new layer/distance).
- Step 6: Get the size of the queue (number of cells to process at the current distance).
- Step 7: Iterate 'q_size' times. In each iteration:
- Step 8: Dequeue a cell (r, c) from the queue.
- Step 9: Explore the four adjacent neighbors (up, down, left, right).
- Step 10: For each neighbor (nr, nc):
- Step 11: If the neighbor is within the grid boundaries and is a water cell (value 0):
- Step 12: Mark the neighbor as visited by changing its value in the grid to 1.
- Step 13: Enqueue the neighbor into the queue.
- Step 14: After the BFS is complete, return the final distance.
Key Insights
- Insight 1: Using Breadth-First Search (BFS) is efficient for finding the shortest distance from multiple source points (land cells) to all water cells.
- Insight 2: Treating all land cells as the starting points for a multi-source BFS helps find the maximum distance to the farthest water cell.
- Insight 3: The problem can be visualized as expanding outwards from the land cells in all four directions, layer by layer, until all reachable water cells are visited.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
Topics
This problem involves: Array, Dynamic Programming, Breadth-First Search, Matrix.
Companies
Asked at: UiPath, Wix.