LeetCode 2812: Find the Safest Path in a Grid Solution

Master LeetCode problem 2812 (Find the Safest 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.

Problem Explanation

Explanation

To find the safest path in a grid, we can start from the end cell (n-1, n-1) and perform a breadth-first search (BFS) while keeping track of the Manhattan distance to the nearest thief cell. This way, we can determine the maximum safeness factor of all paths leading to the end cell.

  1. Initialize a queue for BFS and a 2D array to store the safeness factor for each cell.
  2. Start BFS from the end cell (n-1, n-1) and move towards the start cell.
  3. Update the safeness factor for each cell based on the Manhattan distance to the nearest thief cell.
  4. Finally, return the safeness factor of the start cell (0, 0) which represents the maximum safeness factor of all paths leading to the end cell.

Time Complexity: O(n^2) where n is the size of the grid. Space Complexity: O(n^2) for the safeness factor array and the queue.

Solution Code

class Solution {
    public int safestPath(int[][] grid) {
        int n = grid.length;
        int[][] safeness = new int[n][n];

        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{n - 1, n - 1});

        int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

        while (!queue.isEmpty()) {
            int[] curr = queue.poll();
            int currR = curr[0];
            int currC = curr[1];

            for (int[] dir : dirs) {
                int nextR = currR + dir[0];
                int nextC = currC + dir[1];

                if (nextR >= 0 && nextR < n && nextC >= 0 && nextC < n) {
                    int dist = grid[nextR][nextC] == 1 ? 0 : safeness[currR][currC] + 1;

                    if (safeness[nextR][nextC] == 0 || dist < safeness[nextR][nextC]) {
                        safeness[nextR][nextC] = dist;
                        queue.offer(new int[]{nextR, nextC});
                    }
                }
            }
        }

        return safeness[0][0];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2812 (Find the Safest Path in a Grid)?

This page provides optimized solutions for LeetCode problem 2812 (Find the Safest 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 2812 (Find the Safest Path in a Grid)?

The time complexity for LeetCode 2812 (Find the Safest 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 2812 on DevExCode?

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

Back to LeetCode Solutions