1810. Minimum Path Cost in a Hidden Grid


Problem Description

In this problem, you are tasked with finding the minimum total cost for a robot to move from its starting cell to a target cell in an unknown and potentially partially blocked grid. The grid is represented by a two-dimensional array with m rows and n columns. Each cell in the grid has a cost associated with moving to it, except for the starting cell which does not incur a cost before the robot moves.

The grid is hidden and inaccessible directly. The only interaction with the grid is allowed through a provided GridMaster class, which contains methods that let you check if the robot can move in a certain direction, move the robot in a direction (returning the cost of that cell), and check if the current cell is the target cell.

Here are the key points:

  1. You don't know the size of the grid or the location of the starting and target cells.
  2. The robot should move to the target cell with the minimum total cost.
  3. You may only interact with the grid using the provided GridMaster class functions.
  4. You must determine if there is a path to the target, and if so, calculate its cost. If no path exists, return -1.

Flowchart Walkthrough

Let's go through the Flowchart step-by-step to analyze how to use the Depth-First Search algorithm for LeetCode 1810. Minimum Path Cost in a Hidden Grid:

Is it a graph?

  • Yes: The task involves navigating a hidden grid to find the minimum path cost, which can be represented as a graph where each cell is a node and connections between adjacent cells are edges.

Is it a tree?

  • No: The grid structure generally allows cycles and does not conform to the hierarchical structure of a tree.

Is the problem related to directed acyclic graphs (DAG)?

  • No: The problem involves navigating a hidden grid to find paths, which requires exploring uncertain paths and does not assure a DAG, as backtracking to previously visited cells might be necessary.

Is the problem related to shortest paths?

  • Yes: The core problem is to find the minimum path cost, which is essentially finding a shortest path in terms of cost from one cells to another.

Is the graph weighted?

  • Yes: Since the costs can differ for different paths or steps, the grid should be considered as a weighted graph, where the weight represents the cost of moving from one cell to another.

Does the flowchart lead directly to Depth-First Search?

  • No, the current flowchart insists on using Dijkstra's Algorithm based on the presence of weights and the need to find a shortest path. However, DFS could still be utilized in specific contexts within a broader strategy (perhaps in combination with other methods), but the flowchart would direct us towards Dijkstra’s or other path-finding algorithms in usual circumstances that handle weights more suitably.

Conclusion: Following the Flowchart, this problem should ideally employ Dijkstra's Algorithm due to the weighted shortest path nature of the problem. Depth-First Search is not directly suggested for this problem based on the flowchart guidelines; however, DFS might be used in a hybrid or preliminarily exploration manner within the problem if constraints or specific subproblems within the larger task allow.

Intuition

To solve this problem, we need a two-pronged approach. First, we must map out the grid as much as we can to identify the target cell and the costs of moving through empty cells. Second, once we have mapped the grid, we need to find the shortest path from the start cell to the target cell in terms of cost, not distance.

Since we can only interact with the grid through the GridMaster, we can use depth-first search (DFS) to explore the grid. We need to keep track of the cells we have visited to avoid infinite loops and excessive querying of the GridMaster. Essentially, the process would involve these steps:

  1. Start from the initial cell (where the robot is), mark it as visited, and explore all four directions (U, D, L, R - up, down, left, right).
  2. Use the canMove function to check if the robot can move in a specific direction. If it can, use the move function to move the robot, mark the new cell as visited, record the cost, and recursively continue the process from the new cell.
  3. If the isTarget function returns true, we've found the target cell, so we record its location.
  4. After mapping out the accessible grid (or as far as we can go without hitting blocked cells or grid boundaries), we then apply a shortest-path algorithm, like Dijkstra’s algorithm, to find the path with the minimal cost from the start cell to the target cell. This part of the problem is where we use a priority queue to explore the least costly paths first.

The grid is large, so it makes sense to offset the initial cell to the middle of our grid representation (e.g., if we use a 2D array of size 200x200, we start at position (100, 100)). This choice is because the grid size is unknown, and we need room to explore in all directions. In the shortest-path stage, we use a priority queue to ensure we're always exploring the path with the minimal cost so far, making the process efficient.

To sum up, the intuition is to map out the grid using DFS and GridMaster and then use Dijkstra's algorithm to calculate the minimum total cost to reach the target cell if possible. If the target cell was not located during the mapping phase, or if there is no valid path, we return -1 as specified by the problem.

Learn more about Depth-First Search, Breadth-First Search, Graph and Heap (Priority Queue) patterns.

Solution Approach

The solution consists of two primary components – mapping the grid and then finding the shortest path to the target.

Mapping the Grid

To map out the grid, we initialize a two-dimensional list g of size N x N, with N being large enough to ensure that the robot can explore the grid in all directions from the initial position, which we offset to the center ((100, 100) in this case). Initially, all values in g are set to -1, which denotes unexplored cells.

We define a recursive function dfs(i, j) that accepts the current coordinates (i, j) of the robot in the grid. For each direction (U, D, L, R), the function performs the following steps:

  1. It checks if the robot can move in the current direction using master.canMove(dir).
  2. If the move is possible, it moves the robot with master.move(dir), records the cost of the new cell in g, and calls dfs(x, y) recursively with the updated coordinates.
  3. If the master.isTarget() returns true, the coordinates of the target cell are stored in the target variable.

To prevent moving outside the initialized grid, we check that the new coordinates are within bounds (0 <= x < N and 0 <= y < N) and that the cell has not been visited before (g[x][y] == -1).

After exploring in one direction and completing the depth-first search, we move the robot back to its previous position using master.move(ndir) to backtrack, where ndir is the opposite direction.

Finding the Shortest Path

Once the grid is mapped, we proceed to the shortest-path part of the algorithm. We use Dijkstra's algorithm for this purpose:

  1. We initialize a priority queue q and add the start cell (cost, i, j) with the initial cost set to 0.
  2. We also initialize a dist list to keep track of the minimum cost required to reach each cell, setting the initial cell's cost to 0 and the rest to infinity (INF).
  3. While the priority queue q is not empty, extract the cell with the minimum cost (w, i, j).
  4. If (i, j) is the target cell, return the minimum cost w.
  5. Otherwise, for each adjacent cell (x, y), calculate the potential new cost w + g[x][y]. If this cost is less than dist[x][y], update dist[x][y] with the new cost and push the updated cost and coordinates back into the priority queue.

The use of the priority queue ensures that the cell with the currently known lowest cost is always processed next. This prioritization speeds up the search for the shortest path, as paths with a high cost are not processed until necessary.

If the target cell is not found during the initial DFS exploration, or if there is no path from the starting cell to the target cell, the function returns -1. Otherwise, the function returns the cost of the cheapest path.

The key algorithms, data structures, and patterns used in the solution include:

  • Depth-First Search (DFS): To explore the grid and map out the accessible cells.
  • Dijkstra's Algorithm: To find the shortest path to the target in terms of cost.
  • Priority Queue (Min Heap): To efficiently retrieve the next cell to explore based on the lowest known cost (used in conjunction with Dijkstra's algorithm).
  • Backtracking: To ensure that after exploring a path, the robot returns to its previous position.

This approach ensures a thorough exploration of the grid and an efficient calculation of the minimum total cost to reach the target cell.

Ready to land your dream job?

Unlock your dream job with a 2-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's use a small example to illustrate the solution approach. Assume we have a 5x5 grid with the following conditions (actual grid values are not known to the robot, they are only shown here for illustration):

| S | 1 | - | - | - |    S: Start (0,0)
| - | 1 | 1 | - | T |    T: Target
| - | - | 1 | 1 | 1 |    1: Cost to move
| - | 1 | - | - | - |    -: Blocked cell
| - | 1 | 1 | 1 | - |

The robot starts at the top-left corner S and the target is at position T. The costs to move to any cell are all 1s and the blocked cells are represented by -.

Mapping the Grid

  1. The robot starts DFS from (0,0), marking this cell visited in our g array, initially (100, 100).
  2. The robot can move right to (0,1) and marks it in the grid with a cost of 1.
  3. It cannot move up or down as it is the top edge of the grid. The robot attempts to move left but the cell is blocked, as per the assumed grid structure.
  4. Now, the robot tries to move right from (0,1) to (0,2) but it's blocked so it will backtrack to (0,1).
  5. Next, the robot moves down from (0,1) to (1,1) and marks the cell with a cost of 1.
  6. From (1,1), the robot continues to move right, marking cost 1 at (1,2), and moves down to (2,2), then to (3,2), and finally to (4,2), marking each with cost 1.
  7. The robot now can move right from (4,2) to (4,3) and then up to (1,3), marking all those cells with the cost 1. At (1,3), it moves right and discovers the target T at (1,4).

By the end of the DFS, the robot has the following map:

| 0 | 1 | X | X | X |
| X | 1 | 1 | 1 | T |
| X | X | 1 | 1 | 1 |
| X | 1 | X | X | X |
| X | 1 | 1 | 1 | X |

0 denotes the start cell, X is unvisited or blocked, T is the target, and 1 denotes visited cells with associated costs.

Finding the Shortest Path

  1. We initialize the priority queue with the start cell (0, 100, 100) cost and position.
  2. The priority queue pops the start cell, and we check its neighbors. Since they are all either X or have a cost of 1, we add those with cost 1 to the priority queue with their cumulative costs.
  3. The process continues, with the priority queue ensuring cells with the lowest cumulative cost are explored first.
  4. Once the target cell's coordinates are popped from the queue, the algorithm stops and the cost associated with the target cell is the minimum cost.

Following the described approach with the specified grid, the robot would find a path with a total minimum cost of 3 to move from S to T through (1, 1), (1, 2), (1, 3), and reaching the target at (1, 4).

Solution Implementation

1from heapq import heappop, heappush
2
3class Solution(object):
4    def findShortestPath(self, master: 'GridMaster') -> int:
5        # DFS to build the grid and locate the target.
6        def dfs(i, j):
7            nonlocal target
8            # Check if the current position is the target.
9            if master.isTarget():
10                target = (i, j)
11            # Explore all possible directions.
12            for direction, (delta_i, delta_j, opposite) in directions.items():
13                new_i, new_j = i + delta_i, j + delta_j
14                if (0 <= new_i < N) and (0 <= new_j < N) and master.canMove(direction) and grid[new_i][new_j] == -1:
15                    grid[new_i][new_j] = master.move(direction)
16                    dfs(new_i, new_j)
17                    master.move(opposite)
18
19        # Initialize variables.
20        target = (-1, -1)
21        N = 200
22        INF = float('inf')
23        grid = [[-1] * N for _ in range(N)]
24        directions = {
25            'U': (-1, 0, 'D'),
26            'D': (1, 0, 'U'),
27            'L': (0, -1, 'R'),
28            'R': (0, 1, 'L'),
29        }
30
31        # Start DFS from the center of the grid.
32        dfs(100, 100)
33
34        # If the target is not found, return -1.
35        if target == (-1, -1):
36            return -1
37
38        # Dijkstra's algorithm to find the shortest path to the target.
39        priority_queue = [(0, 100, 100)]  # Weight, i, j
40        distances = [[INF] * N for _ in range(N)]
41        distances[100][100] = 0  # Start with distance 0 from the starting point.
42
43        # Process the nodes in the queue.
44        while priority_queue:
45            weight, i, j = heappop(priority_queue)
46            # Check if we've reached the target.
47            if (i, j) == target:
48                return weight
49            # Explore all possible directions.
50            for delta_i, delta_j, _ in directions.values():
51                new_i, new_j = i + delta_i, j + delta_j
52                # Update the distance if a shorter path is found.
53                if (0 <= new_i < N) and (0 <= new_j < N) and grid[new_i][new_j] != -1 and distances[new_i][new_j] > weight + grid[new_i][new_j]:
54                    distances[new_i][new_j] = weight + grid[new_i][new_j]
55                    heappush(priority_queue, (distances[new_i][new_j], new_i, new_j))
56
57        # If the target was not reached, return 0.
58        return 0
59
1import java.util.Arrays;
2import java.util.PriorityQueue;
3import java.util.Comparator;
4
5class Solution {
6    // Direction mappings
7    private static final char[] DIRS = {'U', 'R', 'D', 'L'};
8    private static final char[] OPPOSITE_DIRS = {'D', 'L', 'U', 'R'};
9    private static final int[] DIRS_DELTA = {-1, 0, 1, 0, -1};
10  
11    // Constants for grid size and infinity equivalent for distances
12    private static final int GRID_SIZE = 200;
13    private static final int INF = Integer.MAX_VALUE / 2; // To avoid overflow
14
15    // Grid representation and distances
16    private static int[][] grid = new int[GRID_SIZE][GRID_SIZE];
17    private static int[][] distances = new int[GRID_SIZE][GRID_SIZE];
18  
19    // Variable to store target position
20    private int[] targetPosition;
21
22    /**
23     * Method to find the shortest path to the target using Dijkstra's algorithm.
24     * @param master The GridMaster instance to interact with the grid.
25     * @return The shortest distance to the target or -1 if the target is unreachable.
26     */
27    public int findShortestPath(GridMaster master) {
28        targetPosition = new int[] {-1, -1};
29      
30        // Initialize grid values to -1 and distances to infinity (INF)
31        for (int i = 0; i < GRID_SIZE; ++i) {
32            Arrays.fill(grid[i], -1);
33            Arrays.fill(distances[i], INF);
34        }
35      
36        // Fill in the available paths and target position with a DFS
37        dfs(100, 100, master);
38      
39        // If target is not found, return -1
40        if (targetPosition[0] == -1) {
41            return -1;
42        }
43      
44        // Implement Dijkstra's algorithm to find the shortest path
45        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
46      
47        // Starting point
48        priorityQueue.offer(new int[] {0, 100, 100});
49        distances[100][100] = 0;
50      
51        // Process nodes
52        while (!priorityQueue.isEmpty()) {
53            int[] point = priorityQueue.poll();
54            int weight = point[0], row = point[1], col = point[2];
55          
56            // If target position is reached, return the path weight
57            if (row == targetPosition[0] && col == targetPosition[1]) {
58                return weight;
59            }
60          
61            // Explore all possible directions from the current node
62            for (int k = 0; k < 4; ++k) {
63                int newRow = row + DIRS_DELTA[k], newCol = col + DIRS_DELTA[k + 1];
64              
65                // Check bounds and if the new cell is accessible
66                if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE
67                    && grid[newRow][newCol] != -1 && distances[newRow][newCol] > weight + grid[newRow][newCol]) {
68                    distances[newRow][newCol] = weight + grid[newRow][newCol];
69                    priorityQueue.offer(new int[] {distances[newRow][newCol], newRow, newCol});
70                }
71            }
72        }
73        // The target was not reachable
74        return -1;
75    }
76
77    /**
78     * Depth-first search to map out the reachable cells in the grid.
79     * @param row The starting row coordinate.
80     * @param col The starting column coordinate.
81     * @param master The GridMaster instance to interact with the grid.
82     */
83    private void dfs(int row, int col, GridMaster master) {
84        // Check if the current position is the target
85        if (master.isTarget()) {
86            targetPosition = new int[] {row, col};
87        }
88      
89        // Explore all possible directions
90        for (int k = 0; k < 4; ++k) {
91            char moveDirection = DIRS[k], oppositeDirection = OPPOSITE_DIRS[k];
92            int newRow = row + DIRS_DELTA[k], newCol = col + DIRS_DELTA[k + 1];
93          
94            // Validate new positions and ensure we haven't visited it before
95            if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE && 
96                master.canMove(moveDirection) && grid[newRow][newCol] == -1) {
97                grid[newRow][newCol] = master.move(moveDirection);
98                dfs(newRow, newCol, master);
99                master.move(oppositeDirection); // Move back
100            }
101        }
102    }
103}
104
1#include <vector>
2#include <queue>
3#include <climits>
4#include <algorithm>
5
6class Solution {
7private:
8    // Direction mappings
9    const char DIRS[4] = {'U', 'R', 'D', 'L'};
10    const char OPPOSITE_DIRS[4] = {'D', 'L', 'U', 'R'};
11    const int DIRS_DELTA[5] = {-1, 0, 1, 0, -1};
12  
13    // Constants for grid size and infinity equivalent for distances
14    static constexpr int GRID_SIZE = 200;
15    static constexpr int INF = INT_MAX / 2; // To avoid overflow
16
17    // Grid representation and distances
18    int grid[GRID_SIZE][GRID_SIZE];
19    int distances[GRID_SIZE][GRID_SIZE];
20  
21    // Variable to store target position
22    std::pair<int, int> targetPosition;
23
24public:
25    Solution() {
26        targetPosition = {-1, -1};
27        std::fill(&grid[0][0], &grid[0][0] + GRID_SIZE * GRID_SIZE, -1);
28        std::fill(&distances[0][0], &distances[0][0] + GRID_SIZE * GRID_SIZE, INF);
29    }
30
31    /**
32     * Method to find the shortest path to the target using Dijkstra's algorithm.
33     * @param master The GridMaster instance to interact with the grid.
34     * @return The shortest distance to the target or -1 if the target is unreachable.
35     */
36    int findShortestPath(GridMaster &master) {
37        // Fill in the available paths and target position with a DFS
38        dfs(100, 100, master);
39      
40        // If target is not found, return -1
41        if (targetPosition.first == -1) {
42            return -1;
43        }
44      
45        // Implement Dijkstra's algorithm to find the shortest path
46        auto cmp = [](const std::vector<int> &a, const std::vector<int> &b) {
47            return a[0] > b[0];
48        };
49        std::priority_queue<std::vector<int>, std::vector<std::vector<int>>, decltype(cmp)> priorityQueue(cmp);
50
51        // Starting point
52        priorityQueue.push({0, 100, 100});
53        distances[100][100] = 0;
54
55        // Process nodes
56        while (!priorityQueue.empty()) {
57            std::vector<int> point = priorityQueue.top();
58            priorityQueue.pop();
59            int weight = point[0], row = point[1], col = point[2];
60          
61            // If target position is reached, return the path weight
62            if (row == targetPosition.first && col == targetPosition.second) {
63                return weight;
64            }
65          
66            // Explore all possible directions from the current node
67            for (int k = 0; k < 4; ++k) {
68                int newRow = row + DIRS_DELTA[k], newCol = col + DIRS_DELTA[k + 1];
69              
70                // Check bounds and if the new cell is accessible
71                if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE
72                    && grid[newRow][newCol] != -1 && distances[newRow][newCol] > weight + grid[newRow][newCol]) {
73                    distances[newRow][newCol] = weight + grid[newRow][newCol];
74                    priorityQueue.push({distances[newRow][newCol], newRow, newCol});
75                }
76            }
77        }
78      
79        // The target was not reachable
80        return -1;
81    }
82
83private:
84    /**
85     * Depth-first search to map out the reachable cells in the grid.
86     * @param row The starting row coordinate.
87     * @param col The starting column coordinate.
88     * @param master The GridMaster instance to interact with the grid.
89     */
90    void dfs(int row, int col, GridMaster &master) {
91        // Check if the current position is the target
92        if (master.isTarget()) {
93            targetPosition = {row, col};
94        }
95
96        // Explore all possible directions
97        for (int k = 0; k < 4; ++k) {
98            char moveDirection = DIRS[k], oppositeDirection = OPPOSITE_DIRS[k];
99            int newRow = row + DIRS_DELTA[k], newCol = col + DIRS_DELTA[k + 1];
100          
101            // Validate new positions and ensure we haven't visited it before
102            if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE && 
103                master.canMove(moveDirection) && grid[newRow][newCol] == -1) {
104                grid[newRow][newCol] = master.move(moveDirection);
105                dfs(newRow, newCol, master);
106                master.move(oppositeDirection); // Move back
107            }
108        }
109    }
110};
111
112/**
113 * The GridMaster class will be provided by the problem in LeetCode and is used here
114 * just for the code to be syntactically correct. This provides the methods used to query
115 * the state of the grid and move within it.
116 */
117class GridMaster {
118public:
119    bool canMove(char direction);
120    int move(char direction);
121    bool isTarget();
122};
123
1// Utility types for direction mappings
2const DIRS: char[] = ['U', 'R', 'D', 'L'];
3const OPPOSITE_DIRS: char[] = ['D', 'L', 'U', 'R'];
4const DIRS_DELTA: number[] = [-1, 0, 1, 0, -1];
5
6// Constants for grid size and infinity equivalent for distances
7const GRID_SIZE: number = 200;
8const INF: number = Number.MAX_SAFE_INTEGER / 2; // To prevent overflow
9
10// Grid representation and distances
11let grid: number[][] = Array.from({ length: GRID_SIZE }, () => new Array(GRID_SIZE).fill(-1));
12let distances: number[][] = Array.from({ length: GRID_SIZE }, () => new Array(GRID_SIZE).fill(INF));
13
14// Variable to store the target position
15let targetPosition: number[] = [-1, -1];
16
17// GridMaster interface as a placeholder for actual GridMaster interactions
18interface GridMaster {
19  canMove(direction: char): boolean;
20  move(direction: char): number;
21  isTarget(): boolean;
22}
23
24// Method to find the shortest path to target using Dijkstra's algorithm
25function findShortestPath(master: GridMaster): number {
26  targetPosition = [-1, -1];
27
28  // Fill in the available paths and target position with a DFS
29  dfs(100, 100, master);
30
31  // If target is not found, return -1
32  if (targetPosition[0] === -1) {
33    return -1;
34  }
35
36  // PriorityQueue setup with a comparator for shortest distance at the top
37  const priorityQueue: [number, number, number][] = [];
38
39  // Starting point
40  enqueue(priorityQueue, 0, 100, 100);
41  distances[100][100] = 0;
42
43  // Process nodes
44  while (priorityQueue.length > 0) {
45    const [weight, row, col] = dequeue(priorityQueue);
46
47    // If target position is reached, return the path weight
48    if (row === targetPosition[0] && col === targetPosition[1]) {
49      return weight;
50    }
51
52    // Explore all possible directions from the current node
53    for (let k = 0; k < 4; ++k) {
54      const newRow = row + DIRS_DELTA[k];
55      const newCol = col + DIRS_DELTA[k + 1];
56
57      // Check bounds and if the new cell is accessible
58      if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE
59        && grid[newRow][newCol] !== -1 && distances[newRow][newCol] > weight + grid[newRow][newCol]) {
60        distances[newRow][newCol] = weight + grid[newRow][newCol];
61        enqueue(priorityQueue, distances[newRow][newCol], newRow, newCol);
62      }
63    }
64  }
65  // The target was not reachable
66  return -1;
67}
68
69function dfs(row: number, col: number, master: GridMaster): void {
70  // Check if the current position is the target
71  if (master.isTarget()) {
72    targetPosition = [row, col];
73  }
74
75  // Explore all possible directions
76  for (let k = 0; k < 4; k++) {
77    const moveDirection = DIRS[k];
78    const oppositeDirection = OPPOSITE_DIRS[k];
79    const newRow = row + DIRS_DELTA[k];
80    const newCol = col + DIRS_DELTA[k + 1];
81
82    // Validate new positions and ensure we haven't visited it before
83    if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE && 
84      master.canMove(moveDirection) && grid[newRow][newCol] === -1) {
85      grid[newRow][newCol] = master.move(moveDirection);
86      dfs(newRow, newCol, master);
87      master.move(oppositeDirection); // Move back
88    }
89  }
90}
91
92function enqueue(queue: [number, number, number][], weight: number, row: number, col: number): void {
93  queue.push([weight, row, col]);
94  queue.sort((a, b) => a[0] - b[0]);
95}
96
97function dequeue(queue: [number, number, number][]): [number, number, number] {
98  return queue.shift() as [number, number, number];
99}
100

Time and Space Complexity

Time Complexity

The time complexity of the solution is determined by the depth-first search (DFS) used to explore the grid and the subsequent use of Dijkstra's algorithm to find the shortest path to the target.

  1. DFS: The DFS function explores potentially every cell once. In the worst case, we could think of this as a recursive call for each accessible cell in the grid. The grid size is bounded by N, so DFS could potentially be O(N^2) in the worst case.

  2. Dijkstra's Algorithm: After DFS execution, the code uses Dijkstra's algorithm with a priority queue (min-heap) for finding the shortest path in the weighted grid. Each cell, if accessible, can be pushed and popped from the priority queue at most once. The heappush and heappop operations work in O(log V), where V is the number of vertices in the queue. In this case, V is at most N^2, so each heap operation is O(log N^2) = O(2 log N) = O(log N). Since we may process every cell once, the time complexity for this part is O(N^2 log N).

Overall, the time complexity combines the complexities of DFS and Dijkstra's algorithm, which yields O(N^2 + N^2 log N). However, the dominant term here is O(N^2 log N), so the time complexity of the entire function is O(N^2 log N).

Space Complexity

The space complexity includes the space used for the recursion call stack in DFS, the grid g, the priority queue in Dijkstra's algorithm, and the dist array.

  1. DFS Stack Space: In the worst case (a completely open grid), the depth of the recursion stack can be O(N^2) since we might need to traverse the entire grid.

  2. Grid g: The grid g is of size N x N, and thus requires O(N^2) space.

  3. Priority Queue: The min-heap used in Dijkstra's algorithm would at most contain all the cells at one time, so it requires O(N^2) space in the worst case.

  4. Distance Array dist: Similarly, this is also a 2D N x N array, which requires O(N^2) space.

Adding all these up, we get a space complexity of O(N^2) as all other factors are also O(N^2).

In conclusion, the provided solution has a time complexity of O(N^2 log N) and a space complexity of O(N^2).

Learn more about how to find time and space complexity quickly using problem constraints.


Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:
Question 1 out of 10

What is the best way of checking if an element exists in a sorted array once in terms of time complexity? Select the best that applies.


Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!


Load More