1162. As Far from Land as Possible Medium
1/**
2 * [1162] As Far from Land as Possible
3 *
4 * 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.
5 * 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|.
6 *
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" />
9 * Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
10 * Output: 2
11 * Explanation: The cell (1, 1) is as far as possible from all the land with distance 2.
12 *
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" />
15 * Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
16 * Output: 4
17 * Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.
18 *
19 *
20 * Constraints:
21 *
22 * n == grid.length
23 * n == grid[i].length
24 * 1 <= n <= 100
25 * grid[i][j] is 0 or 1
26 *
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/as-far-from-land-as-possible/
31// discuss: https://leetcode.com/problems/as-far-from-land-as-possible/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36 pub fn max_distance(grid: Vec<Vec<i32>>) -> i32 {
37 0
38 }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_1162() {
49 }
50}
51
Back
© 2025 bowen.ge All Rights Reserved.