BackmediumBacktrackingAccentureAdobe

Valid Grid Paths Solution

Problem Statement

Given a rectangular grid of size m x n and a set of obstacle coordinates, find all distinct paths from the top-left cell to the bottom-right cell. Movement is restricted to either right or down at any point.

Example 1
Input
grid = [[0,0,0],[0,1,0],[0,0,0]], m = 3, n = 3
Output
[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

Explanation: Step-by-step: We start at the top-left cell (0,0). We can move right to (0,1) or down to (1,0). We explore all paths by backtracking and trying both options. This gives us the correct output.

Example 2
Input
grid = [[0,0,1],[0,1,0],[0,0,0]], m = 3, n = 3
Output
[]

Explanation: Step-by-step: We start at the top-left cell (0,0). We can move right to (0,1) or down to (1,0). However, the path is blocked by an obstacle at (0,2). We cannot move right from (0,1) because it's blocked. We also cannot move down from (1,0) because it's blocked. Therefore, there are no valid paths and the output is an empty list.

Constraints

  • 1 <= m, n <= 20
  • 0 <= number of asteroids <= 10
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Valid Grid Paths — Problem Statement & Solution Guide

BacktrackingMediumMixed
TimeO(m*n)
|
SpaceO(m*n)

Problem Description

Given a rectangular grid of size m x n and a set of obstacle coordinates, find all distinct paths from the top-left cell to the bottom-right cell. Movement is restricted to either right or down at any point.

Examples

Example 1

Input

grid = [[0,0,0],[0,1,0],[0,0,0]], m = 3, n = 3

Output

[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]

Explanation: Step-by-step: We start at the top-left cell (0,0). We can move right to (0,1) or down to (1,0). We explore all paths by backtracking and trying both options. This gives us the correct output.

Example 2

Input

grid = [[0,0,1],[0,1,0],[0,0,0]], m = 3, n = 3

Output

[]

Explanation: Step-by-step: We start at the top-left cell (0,0). We can move right to (0,1) or down to (1,0). However, the path is blocked by an obstacle at (0,2). We cannot move right from (0,1) because it's blocked. We also cannot move down from (1,0) because it's blocked. Therefore, there are no valid paths and the output is an empty list.

Constraints

  • 1 <= m, n <= 20
  • 0 <= number of asteroids <= 10

Optimal Approach & Strategy

An optimal approach would be to use a depth-first search (DFS) or backtracking algorithm, which only explores valid paths that avoid asteroids, resulting in a time complexity of O(m*n).

Brute Force Approach

A naive approach would be to generate all possible paths and check each one against the grid to see if it intersects with an asteroid. This results in a time complexity of O(2^(m+n)) due to the exponential number of possible paths.

Verified Code Solutions

JavaScript Solution
Time: O(m*n)
function validGridPaths(m, n, obstacles) {
  const visited = Array(m).fill(0).map(() => Array(n).fill(false));
  const paths = [];

  function dfs(r, c, path) {
    if (r === m - 1 && c === n - 1) {
      paths.push([...path]);
      return;
    }

    if (r < m && c < n && !obstacles.some(([x, y]) => x === r && y === c) && !visited[r][c]) {
      visited[r][c] = true;
      dfs(r + 1, c, [...path, [r, c]]);
      dfs(r, c + 1, [...path, [r, c]]);
      dfs(r, c, [...path]); // backtrack to explore other paths
      visited[r][c] = false;
    }
  }

  dfs(0, 0, []);
  return paths;
}

Asked in Top Tech Interviews

AccentureAdobe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.