317. Shortest Distance from All Buildings
题目:
You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
- Each 0 marks an empty land which you can pass by freely.
- Each 1 marks a building which you cannot pass through.
- Each 2 marks an obstacle which you cannot pass through.
Example:
Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]] 1 - 0 - 2 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0 Output: 7 Explanation: Given three buildings at(0,0),(0,4),(2,2), and an obstacle at(0,2),he point
t(1,2)is an ideal empty land to build a house, as the total
travel distance of 3+3+1=7 is minimal. So return 7.
Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
链接: http://leetcode.com/problems/maximum-product-of-word-lengths/
题解:
给一块空地,求在空地上新盖一座楼,到其他楼的距离最短。这里我们要用到BFS,就是从每个楼开始用BFS计算空地 "0"到这栋楼的距离,最后把每个空地到每栋楼的距离加起来,求一个最小值。这里我们还要算出楼的数目,仅当空地能连接所有楼的时候,我们才愿意在这块空地上造楼。我们也要维护一个visited矩阵来避免重复。
Time Complexity - O(4mn), Space Complexity - O(mn * k), k为1的数目
public class Solution {
private final int[][] directions = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
public int shortestDistance(int[][] grid) {
if(grid == null || grid.length == 0) {
return Integer.MAX_VALUE;
}
int rowNum = grid.length;
int colNum = grid[0].length;
int[][] distance = new int[rowNum][colNum];
int[][] canReachBuildings = new int[rowNum][colNum];
int buildingNum = 0;
for(int i = 0; i < rowNum; i++) {
for(int j = 0; j < colNum; j++) {
if(grid[i][j] != 0) {
distance[i][j] = Integer.MAX_VALUE;
}
if(grid[i][j] == 1) { // find out all buildings
buildingNum++;
updateDistance(grid, distance, canReachBuildings, i, j);
}
}
}
int min = Integer.MAX_VALUE;
for(int i = 0; i < rowNum; i++) {
for(int j = 0; j < colNum; j++) {
if(canReachBuildings[i][j] == buildingNum) {
min = Math.min(distance[i][j], min);
}
}
}
return min == Integer.MAX_VALUE ? -1 : min;
}
private void updateDistance(int[][] grid, int[][] distance, int[][] canReachBuildings, int row, int col) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{row, col});
boolean[][] visited = new boolean[grid.length][grid[0].length];
visited[row][col] = true;
int dist = 0;
int curLevel = 1;
int nextLevel = 0;
while(!queue.isEmpty()) {
int[] position = queue.poll();
distance[position[0]][position[1]] += dist;
curLevel--;
for(int[] direction : directions) {
int x = position[0] + direction[0];
int y = position[1] + direction[1];
if(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != 0) {
continue;
}
if(!visited[x][y]) {
queue.offer(new int[]{x, y});
nextLevel++;
visited[x][y] = true;
canReachBuildings[x][y]++;
}
}
if(curLevel == 0) {
curLevel = nextLevel;
nextLevel = 0;
dist++;
}
}
}
}
Reference:
https://leetcode.com/discuss/74453/36-ms-c-solution
https://leetcode.com/discuss/74422/clean-solution-easy-understanding-with-simple-explanation
https://leetcode.com/discuss/74999/java-solution-with-explanation-and-time-complexity-analysis
https://leetcode.com/discuss/74380/my-bfs-java-solution
317. Shortest Distance from All Buildings的更多相关文章
- [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- LeetCode 317. Shortest Distance from All Buildings
原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...
- [Locked] Shortest Distance from All Buildings
Shortest Distance from All Buildings You want to build a house on an empty land which reaches all bu ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- Shortest Distance from All Buildings
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- LeetCode Shortest Distance from All Buildings
原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...
- [Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Distance from All Buildings Solution
之前听朋友说LeetCode出了一道新题,但是一直在TLE,我就找时间做了一下.这题是一个比较典型的BFS的题目,自己匆忙写了一个答案,没有考虑优化的问题,应该是有更好的解法的. 原题如下: You ...
随机推荐
- CentOS安装Git实现多人同步开发
描 述 要开发一个"cms系统",有2个人分别是:晓飞, 盈月.要求使用Git来进行版本控制. 项目信息 版本控制:Git 项目名称:cms 开发人员:xiaofei,yingyu ...
- Window VNC远程控制LINUX:VNC详细配置介绍
Window VNC远程控制LINUX:VNC详细配置介绍 //---------------------------------------vnc linux下的详细配置 1.VNC的启动/停止/重 ...
- Careercup - Microsoft面试题 - 5684901156225024
2014-05-10 23:45 题目链接 原题: Arrange the numbers in an array in alternating order. For example if the a ...
- 3527: [Zjoi2014]力 - BZOJ
题目大意:给出n个数qi,定义 Fj为 令 Ei=Fi/qi,求Ei. 看了很久题解,终于有些眉目,因为知道要用FFT,所以思路就很直了 其实我们就是要±1/(j-i)^2 ( j-i大 ...
- phyreengine 3.12.0 安装遇到的问题
发现他们文档都是旧的....略渣阿 需要安装vs2012 update4 vs2013update4 nvdia cg toolkits 3.1 以及 windows SDK 8.1 编译运行第一个s ...
- sql server 数据页缓冲区的内存瓶颈分析
查看数据库的计数器: SELECT * FROM sys.dm_os_performance_counters **也可以使用系统的性能计监测器查看. 右键图表-> 添加计数器. ...
- SSRF攻击实例解析
ssrf攻击概述 很多web应用都提供了从其他的服务器上获取数据的功能.使用用户指定的URL,web应用可以获取图片,下载文件,读取文件内容等.这个功能如果被恶意使用,可以利用存在缺陷的web应用作为 ...
- css内边距与外边距的区别
你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用 padding而不是margin?你知道负margin吗?你 ...
- C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 请教DotNetBar控件中的CalendarView控件如何拖动当前的时间轴
本人想拖动那个当前的时间轴或者让时间轴变动,因为那个时间轴默认的是当前时间.(就是那个黄色的线)