当前位置:网站首页>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
}
}
边栏推荐
- Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- Computing level network notes
- Modify idea code
- Serializer rewrite: update and create methods
- Discussion on enterprise informatization construction
- Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
- Introduction to the basic application and skills of QT
- Integrated use of interlij idea and sonarqube
- The "booster" of traditional office mode, Building OA office system, was so simple!
猜你喜欢

Redis learning (I)
![[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points](/img/c1/045ca010b212376dc3e5532d25c654.png)
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
![[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?

Django operates Excel files through openpyxl to import data into the database in batches.
![[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)](/img/fd/c0f885cdd17f1d13fdbc71b2aea641.jpg)
[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)

Go language - Reflection
![[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis](/img/c6/5f723d9021cf684dcfb662ed3acaec.png)
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis

2022-2-14 learning the imitation Niuke project - send email

【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling

Hudi 数据管理和存储概述
随机推荐
Install database -linux-5.7
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
We have a common name, XX Gong
Derivation of Fourier transform
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
Principles of computer composition - cache, connection mapping, learning experience
LeetCode 513. Find the value in the lower left corner of the tree
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
On February 14, 2022, learn the imitation Niuke project - develop the registration function
Filter comments to filter out uncommented and default values
Bert install no package metadata was found for the 'sacraments' distribution
Integrated use of interlij idea and sonarqube
Jenkins learning (II) -- setting up Chinese
Go language - IO project
ERROR: certificate common name “*.” doesn’t match requested ho
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
Hudi learning notes (III) analysis of core concepts
2022-2-14 learning xiangniuke project - generate verification code
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)