Skip to main content

Minimum Cost Path with Teleportations - Solution & Explanation

HardArrayDynamic ProgrammingMatrix18 min readAsked at: Amazon, Meta, Visa +1
Practice this problem

Problem Statement

You are given a m x n 2D integer array grid and an integer k. You start at the top-left cell (0, 0) and your goal is to reach the bottom‐right cell (m - 1, n - 1).

There are two types of moves available:

  • Normal move: You can move right or down from your current cell (i, j), i.e. you can move to (i, j + 1) (right) or (i + 1, j) (down). The cost is the value of the destination cell.

  • Teleportation: You can teleport from any cell (i, j), to any cell (x, y) such that grid[x][y] <= grid[i][j]; the cost of this move is 0. You may teleport at most k times.

Return the minimum total cost to reach cell (m - 1, n - 1) from (0, 0).

 

Example 1:

Input: grid = [[1,3,3],[2,5,4],[4,3,5]], k = 2

Output: 7

Explanation:

Initially we are at (0, 0) and cost is 0.

Current Position Move New Position Total Cost
(0, 0) Move Down (1, 0) 0 + 2 = 2
(1, 0) Move Right (1, 1) 2 + 5 = 7
(1, 1) Teleport to (2, 2) (2, 2) 7 + 0 = 7

The minimum cost to reach bottom-right cell is 7.

Example 2:

Input: grid = [[1,2],[2,3],[3,4]], k = 1

Output: 9

Explanation:

Initially we are at (0, 0) and cost is 0.

Current Position Move New Position Total Cost
(0, 0) Move Down (1, 0) 0 + 2 = 2
(1, 0) Move Right (1, 1) 2 + 3 = 5
(1, 1) Move Down (2, 1) 5 + 4 = 9

The minimum cost to reach bottom-right cell is 9.

 

Constraints:

  • 2 <= m, n <= 80
  • m == grid.length
  • n == grid[i].length
  • 0 <= grid[i][j] <= 104
  • 0 <= k <= 10

Approach Overview

Problem Overview: You are given a cost matrix where moving through cells accumulates cost, but certain cells allow teleportation to other positions. The goal is to reach the destination with the minimum possible total cost while accounting for both normal moves and teleport transitions.

Approach 1: Brute Force DFS with Backtracking (Exponential Time, O(2^(mn)) time, O(mn) space)

The most direct approach explores every possible path from the start cell to the destination. From each cell you recursively try all valid directions and any teleportation options. You maintain the running path cost and track the minimum cost found. This approach demonstrates the full search space but quickly becomes infeasible because the number of paths grows exponentially with grid size. Space complexity is O(mn) due to the recursion stack and visited tracking.

Approach 2: DFS with Memoization (Top-Down Dynamic Programming) (O(mn * k) time, O(mn) space)

You can eliminate repeated work by caching results for each cell. Instead of recomputing the minimum cost from the same position multiple times, store the result in a DP table. When recursion revisits that cell, return the cached value immediately. Teleportation transitions are handled as additional edges in the recursion. This turns the brute force search into a dynamic programming problem over the grid state space. The complexity becomes O(mn * k), where k represents teleport destinations associated with a cell.

Approach 3: DP with Teleport Mapping and Dijkstra (Optimal) (O(mn log(mn)) time, O(mn) space)

The grid can be modeled as a weighted graph where each cell is a node. Adjacent moves add cost equal to the destination cell, and teleportation creates additional edges connecting distant cells. Running Dijkstra’s algorithm from the start ensures the minimum cost to each cell is processed in increasing order. A priority queue always expands the cheapest state first. To keep teleport operations efficient, group teleport cells using a hash map keyed by teleport identifier stored in an array or grid structure. Each teleport group is processed once to avoid redundant edges.

This approach works well because every grid transition becomes a simple edge relaxation. The algorithm iterates through neighbors in the matrix, pushes candidate states into the priority queue, and updates the distance table when a cheaper path is discovered. The final DP table stores the minimum cost for every cell.

Recommended for interviews: Interviewers usually expect the graph-based dynamic programming solution. Modeling the grid as a weighted graph and applying Dijkstra shows strong understanding of dynamic programming and shortest path techniques. Mentioning the brute force search first demonstrates you understand the problem space, while the optimized DP/priority-queue solution shows practical algorithm design.

Solution

We define f[t][i][j] as the minimum cost to reach cell (i, j) using exactly t teleportations. Initially, f[0][0][0] = 0, and all other states are infinity.

First, we need to initialize f[0][i][j]. Without using teleportation, we can only reach cell (i, j) by moving right or down.

If i > 0, we can move from the cell above (i-1, j), updating the state as:

$f[0][i][j] = min(f[0][i][j], f[0][i-1][j] + grid[i][j])

If j > 0, we can move from the cell to the left (i, j-1), updating the state as:

f[0][i][j] = min(f[0][i][j], f[0][i][j-1] + grid[i][j])

To handle teleportation, we need to group the cells in the grid by their values. We use a hash map g, where the key is the cell value and the value is a list of coordinates of cells with that value.

For each teleportation count t from 1 to k, we process each group in descending order of cell values. For each cell (i, j) in a group, we first update a global minimum mn, representing the minimum cost to reach these cells using t-1 teleportations:

mn = min(mn, f[t-1][i][j])

Then, we update the state of all cells in the group to mn, representing the minimum cost to reach these cells via teleportation.

Next, we traverse the entire grid again to update f[t][i][j], considering moves from the top or left cells:

If i > 0, then:

f[t][i][j] = min(f[t][i][j], f[t][i-1][j] + grid[i][j])

If j > 0, then:

f[t][i][j] = min(f[t][i][j], f[t][i][j-1] + grid[i][j])

Finally, our answer is min(f[t][m-1][n-1]), where t ranges from 0 to k.

The time complexity is O((k + log mn) times mn), and the space complexity is O(k times mn). Here, m and n are the number of rows and columns of the grid, respectively, and k$ is the maximum allowed number of teleportations.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force DFSO(2^(mn))O(mn)Conceptual baseline or very small grids
DFS + Memoization (Top-Down DP)O(mn * k)O(mn)When teleport groups are small and recursion is acceptable
Dijkstra with Teleport MappingO(mn log(mn))O(mn)General case with weighted cells and teleport edges

Video Solution

Minimum Cost Path with Teleportations | LeetCode 3651 | Complete Intuition ExplainedSanyam IIT Guwahati6,692 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Cost Path with Teleportations easy or hard?
Minimum Cost Path with Teleportations is considered a Hard problem because it combines matrix traversal, dynamic programming ideas, and graph shortest-path algorithms. Handling teleport edges efficiently is the key challenge.
Minimum Cost Path with Teleportations Python/Java solution
Implementations typically build a distance matrix and run Dijkstra using a min-heap. Python uses heapq, Java uses PriorityQueue, and C++ uses a priority_queue with a custom comparator. Each iteration relaxes neighboring cells and teleport edges until the destination cost is finalized.
How to solve Minimum Cost Path with Teleportations in O(n)?
A strict O(n) solution generally isn't feasible because the grid behaves like a weighted graph requiring shortest path computation. Most optimal implementations run in O(mn log(mn)) using Dijkstra. If all edge weights are equal, a BFS-style approach could reduce complexity to O(mn).
What is the best approach for Minimum Cost Path with Teleportations?
The most reliable approach models the grid as a weighted graph and runs Dijkstra’s algorithm. Each cell is a node, adjacent moves form edges, and teleportation cells add additional transitions. Using a priority queue ensures the minimum cost path is expanded first. This solution runs in O(mn log(mn)) time and O(mn) space.
Is Minimum Cost Path with Teleportations asked at Google/Amazon/Meta?
Grid shortest-path problems with teleportation or portals frequently appear in interviews at companies like Google, Amazon, and Meta. Variants combine matrix traversal with graph algorithms or dynamic programming. Candidates are expected to recognize the shortest-path structure quickly.
What data structure is used in Minimum Cost Path with Teleportations?
The optimal solution uses a priority queue (min-heap) for Dijkstra’s algorithm, along with a DP or distance matrix to store the best cost for each cell. A hash map or grouped array structure is often used to manage teleportation pairs efficiently.
What is the time complexity of Minimum Cost Path with Teleportations?
The optimal solution using Dijkstra with a priority queue runs in O(mn log(mn)) time for an m x n grid. Each cell can be processed once while the priority queue maintains candidate states. The space complexity is O(mn) for the distance table and queue.

Ready to solve this problem?

Practice Minimum Cost Path with Teleportations with our built-in code editor and test cases.

Practice on FleetCode