Similar Problems

Similar Problems not available

Reachable Nodes With Restrictions - Leetcode Solution

Companies:

LeetCode:  Reachable Nodes With Restrictions Leetcode Solution

Difficulty: Medium

Topics: hash-table depth-first-search union-find breadth-first-search tree array graph  

Problem Statement:

You are given a grid with rows numbered from 0 to m-1 and columns numbered from 0 to n-1. You are also given an array restricted of size k where (restricted[i][0], restricted[i][1]) indicates that cell (restricted[i][0], restricted[i][1]) is restricted. You want to find the maximum number of cells you can visit in the grid, starting at cell (0, 0) and moving in any of the four cardinal directions (up, down, left, or right), without crossing any of the cells indicated as restricted.

Approach:

We can model this problem as a graph problem where each cell represents a vertex and the edges connect two cells if they are adjacent to each other and are not restricted. We can then run a modified BFS traversal of the graph, where we keep track of the number of cells visited so far and update the count when we encounter a new cell.

For the modified BFS traversal, we can use a queue to keep track of the cells visited so far. We start by adding cell (0, 0) to the queue and mark it as visited. Then, in each iteration of the BFS traversal, we remove a cell from the queue, check its neighbors, and add them to the queue and mark them as visited if they are not restricted and have not been visited before.

We also maintain a separate 2D array visited[][] to keep track of the cells visited so far.

Code:

We can start by defining a function to check whether a cell is valid or not:

def is_valid(i, j, restricted):
    if [i, j] in restricted:
        return False
    return True

Next, we can define our BFS function:

from collections import deque

def bfs(m, n, restricted):
    queue = deque([(0, 0)]) # Start from cell (0, 0)
    visited = [[False for j in range(n)] for i in range(m)] # Keep track of visited cells
    visited[0][0] = True # Mark (0, 0) as visited
    count = 1 # Start with 1, since we have already visited cell (0, 0)

    while queue:
        i, j = queue.popleft() # Remove a cell from the queue
        neighbors = [(i-1, j), (i+1, j), (i, j-1), (i, j+1)] # Get the neighboring cells

        # Check if a neighbor is valid and has not been visited before
        for ni, nj in neighbors:
            if 0 <= ni < m and 0 <= nj < n and is_valid(ni, nj, restricted) and not visited[ni][nj]:
                queue.append((ni, nj)) # Add the neighbor to the queue
                visited[ni][nj] = True # Mark the neighbor as visited
                count += 1 # Increment the count

    return count

Finally, we can use the above BFS function to solve the problem:

def reachable_nodes(m, n, restricted):
    count = bfs(m, n, restricted) # Get the number of reachable cells
    for i, j in restricted:
        # For each restricted cell, check if its neighbors are reachable, and add them to the count
        neighbors = [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]
        for ni, nj in neighbors:
            if 0 <= ni < m and 0 <= nj < n and is_valid(ni, nj, restricted) and not visited[ni][nj]:
                count += 1
    return count

Complexity:

The time complexity of the above solution is O(mn), where m is the number of rows and n is the number of columns. This is because we visit each cell in the grid at most once. The space complexity of the solution is also O(mn), since we use a separate 2D array to keep track of the visited cells.

Reachable Nodes With Restrictions Solution Code

1