LeetCode 1391: Check if There is a Valid Path in a Grid Solution

Master LeetCode problem 1391 (Check if There is a Valid Path in a Grid), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1391. Check if There is a Valid Path in a Grid

Problem Explanation

Explanation:

To solve this problem, we can perform a depth-first search (DFS) traversal on the grid starting from the upper-left cell (0,0) and checking if we can reach the bottom-right cell (m-1, n-1) following the allowed paths. We need to keep track of the directions we can move from each cell based on the street type.

  1. Create a helper function dfs to perform DFS traversal from a given cell.
  2. Use a boolean visited array to keep track of visited cells.
  3. Implement logic to determine the valid directions we can move based on the street type at each cell.
  4. Recursively explore all possible paths from the current cell to its neighbors.
  5. Return true if we reach the bottom-right cell, otherwise return false.

Time Complexity: The time complexity of this approach is O(m * n) as we potentially visit each cell once in the worst case.

Space Complexity: The space complexity is O(m * n) for the visited array.

Solution Code

class Solution {
    public boolean hasValidPath(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        boolean[][] visited = new boolean[m][n];
        return dfs(grid, 0, 0, visited);
    }
    
    private boolean dfs(int[][] grid, int i, int j, boolean[][] visited) {
        int m = grid.length;
        int n = grid[0].length;
        if (i == m - 1 && j == n - 1) {
            return true;
        }
        if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j]) {
            return false;
        }
        visited[i][j] = true;
        int street = grid[i][j];
        if ((street == 1 && (j > 0 && (grid[i][j - 1] == 1 || grid[i][j - 1] == 3 || grid[i][j - 1] == 5))) ||
            (street == 2 && (i > 0 && (grid[i - 1][j] == 2 || grid[i - 1][j] == 3 || grid[i - 1][j] == 4))) ||
            (street == 3 && (j > 0 && (grid[i][j - 1] == 1 || grid[i][j - 1] == 3 || grid[i][j - 1] == 5)) ||
                           (i < m - 1 && (grid[i + 1][j] == 2 || grid[i + 1][j] == 5 || grid[i + 1][j] == 6))) ||
            (street == 4 && (j < n - 1 && (grid[i][j + 1] == 1 || grid[i][j + 1] == 4 || grid[i][j + 1] == 6)) ||
                           (i < m - 1 && (grid[i + 1][j] == 2 || grid[i + 1][j] == 5 || grid[i + 1][j] == 6))) ||
            (street == 5 && (j > 0 && (grid[i][j - 1] == 1 || grid[i][j - 1] == 3 || grid[i][j - 1] == 5)) ||
                           (i > 0 && (grid[i - 1][j] == 2 || grid[i - 1][j] == 3 || grid[i - 1][j] == 4))) ||
            (street == 6 && (j < n - 1 && (grid[i][j + 1] == 1 || grid[i][j + 1] == 4 || grid[i][j + 1] == 6)) ||
                           (i > 0 && (grid[i - 1][j] == 2 || grid[i - 1][j] == 3 || grid[i - 1][j] == 4)))) {
            return dfs(grid, i, j + 1, visited) ||
                   dfs(grid, i, j - 1, visited) ||
                   dfs(grid, i + 1, j, visited) ||
                   dfs(grid, i - 1, j, visited);
        }
        return false;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1391 (Check if There is a Valid Path in a Grid)?

This page provides optimized solutions for LeetCode problem 1391 (Check if There is a Valid Path in a Grid) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1391 (Check if There is a Valid Path in a Grid)?

The time complexity for LeetCode 1391 (Check if There is a Valid Path in a Grid) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1391 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1391 in Java, C++, or Python.

Back to LeetCode Solutions