## Similar Problems

Similar Problems not available

# Task Scheduler Ii - Leetcode Solution

LeetCode: Task Scheduler Ii Leetcode Solution

Difficulty: Medium

Topics: hash-table array simulation

Problem Statement:

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.

Solution:

Approach: BFS (Breadth First Search)

Firstly, we need to determine whether it is possible to reach all the buildings from any given land. To do this, we will perform BFS traversal starting from each 1. For each 1, we will determine the distances d[i][j][k] to all lands that it can reach.

We will also keep a count c[i][j] which counts the number of 1s that we can reach from any given land. If we find a land i,j that can reach less than all the buildings, then we cannot build the house there as some buildings will not be reachable.

Algorithm Steps:

- Initialize 3D distance array d[i][j][k], 2D reachability array c[i][j], and total building count buildings.
- Traverse through the 2D grid array: a. For each building do BFS of all reachable empty lands, updating distance array d. b. For each reachable empty land at distance d[i][j][k], increment reachability count for this land in c[i][j]. c. Find the minimum distance to cover all buildings for each empty land point. d. Choose the minimum of the empty land points with the minimum distance.
- Return the minimum distance calculated in step 2(d).

Let's implement this solution in Python.

Python Code:

class Solution: def bfs(self, i, j, grid, d, bldgs): # BFS traversal q = [(i, j, 0)] visited = set() while q: i, j, steps = q.pop(0) if (i, j) in visited or i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 2: continue visited.add((i, j)) if grid[i][j] == 1: # Building bldgs.add((i, j)) # Add as a reachable building d[i][j].append(steps) # Add distance to distance array q.append((i+1, j, steps+1)) q.append((i-1, j, steps+1)) q.append((i, j+1, steps+1)) q.append((i, j-1, steps+1))

```
def shortestDistance(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
d = [[[] for _ in range(cols)] for _ in range(rows)] # 3D array to store distances from each building to each land point
c = [[0] * cols for _ in range(rows)] # 2D array to store the number of buildings we can reach from each land point
bldgs = set() # Set of all building coordinates
# Traverse through the grid, BFS traversal from each building
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
self.bfs(i, j, grid, d, bldgs)
# Traverse through the distance array and update c (reachability array)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 0: # Reachable empty land
for k in range(len(d[i][j])):
c[i][j] += 1 # Increment count of reachable buildings
# Find minimum distance to cover all buildings
minimum = float('inf')
for i in range(rows):
for j in range(cols):
if grid[i][j] == 0 and c[i][j] == len(bldgs): # Reachable empty land with all buildings reachable
minimum = min(minimum, sum(d[i][j])) # Update minimum distance
return -1 if minimum == float('inf') else minimum
```

Time Complexity: O(n^2 * m^2) where n = number of rows and m = number of columns. We do BFS traversal for each building for each land point. Each BFS has time complexity O(nm) as we traverse through the entire grid.

Space Complexity: O(nm(r + c)) where r = maximum number of reachable buildings from a single empty land and c = maximum number of distances from a single building. We store a 3D distance array d which has n * m * r elements, a 2D reachability array c which has n * m elements and a set of all the building coordinates.

## Task Scheduler Ii Solution Code

`1`