LeetCode 3286: Find a Safe Walk Through a Grid Solution

Master LeetCode problem 3286 (Find a Safe Walk Through 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.

Problem Explanation

Explanation:

To solve this problem, we can use Depth-First Search (DFS) to explore all possible paths from the start cell to the end cell while keeping track of the health remaining at each step. If we reach the destination cell with positive health, we return true; otherwise, we return false. We need to mark the cells already visited to avoid revisiting them in the path.

  1. Create a helper function dfs that takes the current position (x, y), current health remaining, and the grid as parameters.
  2. Check if the current position is out of bounds or if the health is not enough to continue. If so, return false.
  3. Check if the current position is the destination cell. If so, return true.
  4. Mark the current position as visited by setting it to -1 in the grid.
  5. Recursively explore all valid neighboring cells (up, down, left, right) with updated health.
  6. If any of the recursive calls return true, propagate true up the call stack.
  7. If no valid path is found, backtrack by marking the current cell as unvisited and return false.

Time Complexity: O(m * n) where m and n are the dimensions of the grid. Space Complexity: O(m * n) for the recursive call stack.

Solution Code

class Solution {
    public boolean findSafeWalk(int[][] grid, int health) {
        return dfs(0, 0, health, grid);
    }

    private boolean dfs(int x, int y, int health, int[][] grid) {
        if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || health <= 0 || grid[x][y] == -1) {
            return false;
        }

        if (x == grid.length - 1 && y == grid[0].length - 1) {
            return true;
        }

        grid[x][y] = -1;

        boolean result = dfs(x + 1, y, grid);
        result |= dfs(x - 1, y, grid);
        result |= dfs(x, y + 1, grid);
        result |= dfs(x, y - 1, grid);

        grid[x][y] = 0;

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3286 (Find a Safe Walk Through a Grid)?

This page provides optimized solutions for LeetCode problem 3286 (Find a Safe Walk Through 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 3286 (Find a Safe Walk Through a Grid)?

The time complexity for LeetCode 3286 (Find a Safe Walk Through 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 3286 on DevExCode?

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

Back to LeetCode Solutions