Similar Problems

Similar Problems not available

Check If Point Is Reachable - Leetcode Solution

Companies:

LeetCode:  Check If Point Is Reachable Leetcode Solution

Difficulty: Hard

Topics: math  

Problem Statement:

You are given an array of points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. You can move in any direction at each point in the plane. You start at the point [0, 0]. Given an integer maxMove, your task is to check if there exists a path starting from the point [0, 0] and ending at the point [m-1, n-1] such that the total number of moves made is less than or equal to maxMove.

Solution:

Approach:

The idea here is to use Depth First Search (DFS) or Breadth First Search (BFS) to explore all the possible paths and then check if there is any path that can reach the end point [m-1, n-1] within the given maximum distance maxMove. We can start from the point [0, 0] and search for all the possible paths in the order of left, right, up, and down. If we reach the end point within maxMove steps, we can return true, otherwise, we continue exploring all the other paths and return false if no path can reach the end point within maxMove steps.

Algorithm:

  1. Initialize a queue or stack to store the current position and the number of steps taken to reach that position.

  2. Visit the starting point [0, 0] and add it to the queue or stack.

  3. While the queue or stack is not empty, remove the front element of the queue or the top element of the stack and explore all the possible paths from this point.

  4. If we reach the end point [m-1, n-1] within maxMove steps, return true.

  5. Otherwise, for each neighbor of the current point, check if it is within the bounds of the 2D plane and whether we have already visited it or not. If it is not visited, add it to the queue or stack along with the number of steps taken to reach that point.

  6. Repeat steps 3-5 until we have explored all the possible paths from the starting point.

  7. If we have explored all the possible paths and still have not reached the end point within maxMove steps, return false.

Time Complexity:

The time complexity of this algorithm is O(m * n * maxMove) since we are exploring all the possible paths from the starting point. The worst-case scenario would be when we have to explore all the cells on the plane, and the maximum distance we can travel is maxMove.

Space Complexity:

The space complexity of this algorithm is O(m * n * maxMove) since the size of the queue or stack can be up to m * n * maxMove, which is the worst-case scenario when we have to explore all the cells on the plane, and the maximum distance we can travel is maxMove.

Python code implementation:

from collections import deque

class Solution: def is_valid(self, x, y, m, n): if x < 0 or x >= m or y < 0 or y >= n: # Check if the point lies within the bounds of the 2D plane. return False return True

def isReachable(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> bool:
    queue = deque()                             # Use a queue to store the current position and the number of steps taken to reach that position.     
    visited = set()                             # Use a set to keep track of the visited points.
    
    queue.append([startRow, startColumn, 0])     # Visit the starting point and add it to the queue along with the number of steps taken to reach that point.
    visited.add((startRow, startColumn))
    
    while queue:
        x, y, distance = queue.popleft()        # Remove the front element of the queue and explore all the possible paths from this point.
        
        if x == m-1 and y == n-1 and distance <= maxMove:    # If we reach the end point within maxMove steps, return true.
            return True
        
        if distance >= maxMove:                  # If we have traveled more than maxMove steps and still haven't reached the end point, return false since it is impossible to reach the end point within maxMove steps.
            return False
        
        for dx, dy in [[-1,0], [1,0], [0,-1], [0,1]]:
            nx, ny = x+dx, y+dy
            
            if self.is_valid(nx, ny, m, n) and (nx, ny) not in visited:   # For each neighbor of the current point, check if it is within the bounds of the 2D plane and whether we have already visited it or not.
                queue.append([nx, ny, distance+1])                       # If it is not visited, add it to the queue along with the number of steps taken to reach that point.
                visited.add((nx, ny))                                    # Mark it as visited.
    
    return False                                                           # If we have explored all the possible paths and still have not reached the end point within maxMove steps, return false.

Check If Point Is Reachable Solution Code

1