|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/maximum-level-sum-of-a-binary-tree "Maximum Level Sum of a Binary Tree") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/last-substring-in-lexicographical-order "Last Substring in Lexicographical Order") |
| 11 | + |
| 12 | +## 1162. As Far from Land as Possible (Medium) |
| 13 | + |
| 14 | +<p>Given an N x N <code>grid</code> containing only values <code>0</code> and <code>1</code>, where <code>0</code> represents water and <code>1</code> represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.</p> |
| 15 | + |
| 16 | +<p>The distance used in this problem is the <em>Manhattan distance</em>: the distance between two cells <code>(x0, y0)</code> and <code>(x1, y1)</code> is <code>|x0 - x1| + |y0 - y1|</code>.</p> |
| 17 | + |
| 18 | +<p>If no land or water exists in the grid, return <code>-1</code>.</p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | + |
| 22 | +<p><strong>Example 1:</strong></p> |
| 23 | + |
| 24 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" /></strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input: </strong><span id="example-input-1-1">[[1,0,1],[0,0,0],[1,0,1]]</span> |
| 28 | +<strong>Output: </strong><span id="example-output-1">2</span> |
| 29 | +<strong>Explanation: </strong> |
| 30 | +The cell (1, 1) is as far as possible from all the land with distance 2. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>Example 2:</strong></p> |
| 34 | + |
| 35 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" /></strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input: </strong><span id="example-input-2-1">[[1,0,0],[0,0,0],[0,0,0]]</span> |
| 39 | +<strong>Output: </strong><span id="example-output-2">4</span> |
| 40 | +<strong>Explanation: </strong> |
| 41 | +The cell (2, 2) is as far as possible from all the land with distance 4. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +<p><span><strong>Note:</strong></span></p> |
| 47 | + |
| 48 | +<ol> |
| 49 | + <li><span><code>1 <= grid.length == grid[0].length <= 100</code></span></li> |
| 50 | + <li><span><code>grid[i][j]</code> is <code>0</code> or <code>1</code></span></li> |
| 51 | +</ol> |
| 52 | + |
| 53 | +### Related Topics |
| 54 | + [[Breadth-first Search](https://github.com/openset/leetcode/tree/master/tag/breadth-first-search/README.md)] |
| 55 | + [[Graph](https://github.com/openset/leetcode/tree/master/tag/graph/README.md)] |
| 56 | + |
| 57 | +### Similar Questions |
| 58 | + 1. [Shortest Distance from All Buildings](https://github.com/openset/leetcode/tree/master/problems/shortest-distance-from-all-buildings) (Hard) |
| 59 | + |
| 60 | +### Hints |
| 61 | +<details> |
| 62 | +<summary>Hint 1</summary> |
| 63 | +Can you think of this problem in a backwards way ? |
| 64 | +</details> |
| 65 | + |
| 66 | +<details> |
| 67 | +<summary>Hint 2</summary> |
| 68 | +Imagine expanding outward from each land cell. What kind of search does that ? |
| 69 | +</details> |
| 70 | + |
| 71 | +<details> |
| 72 | +<summary>Hint 3</summary> |
| 73 | +Use BFS starting from all land cells in the same time. |
| 74 | +</details> |
| 75 | + |
| 76 | +<details> |
| 77 | +<summary>Hint 4</summary> |
| 78 | +When do you reach the furthest water cell? |
| 79 | +</details> |
0 commit comments