当前位置:网站首页>LeetCode每日一题(1162. As Far from Land as Possible)
LeetCode每日一题(1162. As Far from Land as Possible)
2022-07-03 09:01:00 【wangjun861205】
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 as possible from all the land with distance 2.
Example 2:

Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
Output: 4
Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.
Constraints:
n == grid.length
n == grid[i].length
1 <= n <= 100
grid[i][j] is 0 or 1
impl Solution {
pub fn max_distance(mut grid: Vec<Vec<i32>>) -> i32 {
for i in 0..grid.len() {
for j in 0..grid[0].len() {
if grid[i][j] == 0 {
grid[i][j] = i32::MAX;
continue;
}
grid[i][j] = 0;
}
}
loop {
let mut modified = false;
for i in 0..grid.len() {
for j in 0..grid[0].len() {
let top = if i > 0 {
grid[i - 1][j] } else {
i32::MAX };
let bottom = if i < grid.len() - 1 {
grid[i + 1][j]
} else {
i32::MAX
};
let left = if j > 0 {
grid[i][j - 1] } else {
i32::MAX };
let right = if j < grid[0].len() - 1 {
grid[i][j + 1]
} else {
i32::MAX
};
let base = top.min(bottom).min(left).min(right);
if base == i32::MAX {
continue;
}
if grid[i][j] > base + 1 {
grid[i][j] = base + 1;
modified = true;
}
}
}
if !modified {
break;
}
}
let ans = grid
.into_iter()
.map(|row| row.into_iter().max().unwrap())
.max()
.unwrap();
if ans == 0 || ans == i32::MAX {
return -1;
}
ans
}
}
边栏推荐
- 2022-2-14 learning the imitation Niuke project - send email
- Using Hudi in idea
- Notes on numerical analysis (II): numerical solution of linear equations
- 【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
- Install database -linux-5.7
- [point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
- What are the stages of traditional enterprise digital transformation?
- Construction of simple database learning environment
- [point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
- NPM install installation dependency package error reporting solution
猜你喜欢

Jenkins learning (III) -- setting scheduled tasks
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

On a un nom en commun, maître XX.

State compression DP acwing 291 Mondrian's dream

Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation

【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points

Sword finger offer II 029 Sorted circular linked list

【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
![[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs](/img/0a/480f1d1eea6f2ecf84fd5aa96bd9fb.png)
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs

Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
随机推荐
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
Internet Protocol learning record
【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
Uc/os self-study from 0
Overview of database system
Spark 概述
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Tag paste operator (#)
ERROR: certificate common name “*.” doesn’t match requested ho
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
2022-2-14 learning xiangniuke project - Session Management
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
Hudi 数据管理和存储概述
NPM install installation dependency package error reporting solution
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
State compression DP acwing 91 Shortest Hamilton path
Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
LeetCode 438. Find all letter ectopic words in the string
Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation