当前位置:网站首页>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
}
}
边栏推荐
- [point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
- NPM install installation dependency package error reporting solution
- Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
- 【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
- Hudi学习笔记(三) 核心概念剖析
- 【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
- How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
- Construction of simple database learning environment
- ERROR: certificate common name “*.” doesn’t match requested ho
- LeetCode 532. K-diff number pairs in array
猜你喜欢
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
AcWing 787. Merge sort (template)
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
We have a common name, XX Gong
Basic knowledge of network security
Modify idea code
Idea uses the MVN command to package and report an error, which is not available
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
[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)
Jenkins learning (II) -- setting up Chinese
随机推荐
【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
What is an excellent fast development framework like?
Save the drama shortage, programmers' favorite high-score American drama TOP10
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
LeetCode 871. Minimum refueling times
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
Spark 结构化流写入Hudi 实践
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
Crawler career from scratch (IV): climb the bullet curtain of station B through API
ERROR: certificate common name “*.” doesn’t match requested ho
Recommend a low code open source project of yyds
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
Data mining 2021-4-27 class notes
图像修复方法研究综述----论文笔记
Jenkins learning (III) -- setting scheduled tasks
We have a common name, XX Gong
LeetCode 57. Insert interval
Numerical analysis notes (I): equation root
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code