Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1810: Minimum Path Cost in a Hidden Grid

In this guide, we solve Leetcode #1810 Minimum Path Cost in a Hidden Grid in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

This is an interactive problem. There is a robot in a hidden grid, and you are trying to get it from its starting cell to the target cell in this grid.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Depth-First Search, Breadth-First Search, Graph, Array, Interactive, Matrix, Shortest Path, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

Example

Input: grid = [[2,3],[1,1]], r1 = 0, c1 = 1, r2 = 1, c2 = 0 Output: 2 Explanation: One possible interaction is described below: The robot is initially standing on cell (0, 1), denoted by the 3. - master.canMove('U') returns false. - master.canMove('D') returns true. - master.canMove('L') returns true. - master.canMove('R') returns false. - master.move('L') moves the robot to the cell (0, 0) and returns 2. - master.isTarget() returns false. - master.canMove('U') returns false. - master.canMove('D') returns true. - master.canMove('L') returns false. - master.canMove('R') returns true. - master.move('D') moves the robot to the cell (1, 0) and returns 1. - master.isTarget() returns true. - master.move('L') doesn't move the robot and returns -1. - master.move('R') moves the robot to the cell (1, 1) and returns 1. We now know that the target is the cell (1, 0), and the minimum total cost to reach it is 2.

Python Solution

# """ # This is GridMaster's API interface. # You should not implement it, or speculate about its implementation # """ # class GridMaster(object): # def canMove(self, direction: str) -> bool: # # # def move(self, direction: str) -> int: # # # def isTarget(self) -> bool: # # class Solution(object): def findShortestPath(self, master: "GridMaster") -> int: def dfs(x: int, y: int) -> None: nonlocal target if master.isTarget(): target = (x, y) for k in range(4): dx, dy = dirs[k], dirs[k + 1] nx, ny = x + dx, y + dy if ( 0 <= nx < m and 0 <= ny < n and g[nx][ny] == -1 and master.canMove(s[k]) ): g[nx][ny] = master.move(s[k]) dfs(nx, ny) master.move(s[(k + 2) % 4]) dirs = (-1, 0, 1, 0, -1) s = "URDL" m = n = 200 g = [[-1] * n for _ in range(m)] target = (-1, -1) sx = sy = 100 dfs(sx, sy) if target == (-1, -1): return -1 pq = [(0, sx, sy)] dist = [[inf] * n for _ in range(m)] dist[sx][sy] = 0 while pq: w, x, y = heappop(pq) if (x, y) == target: return w for dx, dy in pairwise(dirs): nx, ny = x + dx, y + dy if ( 0 <= nx < m and 0 <= ny < n and g[nx][ny] != -1 and w + g[nx][ny] < dist[nx][ny] ): dist[nx][ny] = w + g[nx][ny] heappush(pq, (dist[nx][ny], nx, ny)) return -1

Complexity

The time complexity is O(m×nlog⁡(m×n))O(m \times n \log(m \times n))O(m×nlog(m×n)), and the space complexity is O(m×n)O(m \times n)O(m×n). The space complexity is O(m×n)O(m \times n)O(m×n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy