当前位置:网站首页>1162. Map analysis - non recursive method
1162. Map analysis - non recursive method
2022-07-28 21:24:00 【Mr Gao】
1162. Map analysis
Now you have a copy of n x n Of grid grid, Each of the above Cell Use both 0 and 1 It's marked . among 0 On behalf of the ocean ,1 Representing land .
Please find an ocean cell , The ocean cell has the largest distance to the nearest land cell , And return the distance . If there's only land or ocean on the grid , Please return -1.
The distance we're talking about here is 「 Manhattan distance 」( Manhattan Distance):(x0, y0) and (x1, y1) The distance between these two cells is |x0 - x1| + |y0 - y1| .
Example 1:
Input :grid = [[1,0,1],[0,0,0],[1,0,1]]
Output :2
explain :
Ocean cell (1, 1) And the distance between all the land cells reaches the maximum , The maximum distance is 2.
Example 2:
Input :grid = [[1,0,0],[0,0,0],[0,0,0]]
Output :4
explain :
Ocean cell (2, 2) And the distance between all the land cells reaches the maximum , The maximum distance is 4.
For this question , If we don't use recursive methods , Then use two arrays to store 0 and 1 Coordinates of , Then calculate , It can reduce the computational cost , But this way , Not yet optimal , It can be further judged that the calculation stops , The solution code is as follows :
int maxDistance(int** grid, int gridSize, int* gridColSize){
int n=gridSize,m=gridColSize[0];
int store0[n*m][2];
int store1[n*m][2];
int i,j;
int size0=0,size1=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(grid[i][j]==0){
store0[size0][0]=i;
store0[size0][1]=j;
size0++;
}
else{
store1[size1][0]=i;
store1[size1][1]=j;
size1++;
}
}
}
if(size1==n*m||size0==n*m){
return -1;
}
int max=0;
for(i=0;i<size0;i++){
int x=store0[i][0];
int y=store0[i][1];
int min=2000;
for(j=0;j<size1;j++){
int d=abs(store1[j][0]-x)+abs(store1[j][1]-y);
if(d<min){
min=d;
}
}
if(min>max){
max=min;
}
}
return max;
}
边栏推荐
- 关键路径的分析
- Analysis of critical path
- Bug of Dom4j
- Nacos 原理
- protobuf 中基础数据类型的读写
- What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?
- Eureka registers with each other, only showing each other or only showing problems in one
- Applet container technology improves mobile R & D efficiency by 500%
- 什么是 CI/CD? | 实现更快更好的软件交付
- Moco V1: the visual field can also be self supervised
猜你喜欢

How to measure software architecture

IJCAI2022教程 | 对话推荐系统
![[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles](/img/4f/e7806d75cd719e181d8455e4fdc1e7.jpg)
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles

Kubeadm搭建kubernetes集群

A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year

Backup and recovery of SQL Server database

Ijcai2022 tutorial | dialogue recommendation system

九鑫智能正式加入openGauss社区

Database -- use of explain

DELTA热金属检测器维修V5G-JC-R1激光测量传感器/检测仪原理分析
随机推荐
Study - Summary of geometric calculations
ABB electromagnetic flowmeter maintenance signal transmitter maintenance 41f/e4 technical parameters
【Bluetooth蓝牙开发】八、BLE协议之传输层
Niuke turns on the camera and the picture disappears a few seconds later | the picture flashes when the camera is turned on
承载银行关键应用的容器云平台如何选型及建设?
The framing efficiency of setpreviewcallbackwithbuffer will become lower
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Dom4J的Bug
证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
Ctfshow question making web module web11~web14
实习日记第一周
关键路径的分析
Lazada店铺如何产号高效补单?(测评自养号技术详解篇)
Attribute based encryption simulation and code implementation (cp-abe) paper: ciphertext policy attribute based encryption
Go concurrent programming basics
Guanghetong & Qualcomm Internet of things technology open day successfully held
Unity - Fundamentals of 3D mathematics
setPreviewCallbackWithBuffer的出帧效率会变低
探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
CVPR 2022 | in depth study of batch normalized estimation offset in network