Minimum Cost Path with Teleportations Problem
#3651
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3651.
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 thatgrid[x][y] <= grid[i][j]; the cost of this move is 0. You may teleport at mostktimes.
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 <= 80m == grid.lengthn == grid[i].length0 <= grid[i][j] <= 10^40 <= k <= 10
Solution
Approach: Dynamic Programming over Teleport Counts
Let f[t][i][j] be the minimum cost to reach cell (i, j) using exactly t teleports (here we allow "at most" in the end). With no teleports, a cell is reached from above or from the left, as in the classic grid path DP.
For each extra teleport t = 1..k:
- Teleporting goes from a cell with value
>=the destination's value to the destination, for free. So process the distinct cell values from largest to smallest, keep a running minimummnoff[t-1]over all cells seen so far (values at least as large), and setf[t][i][j] = mnfor the cells of the current value. - Then run the plain right/down DP again for layer
t:f[t][i][j] = min(f[t][i][j], f[t][i-1][j] + grid[i][j], f[t][i][j-1] + grid[i][j]).
The answer is the minimum of f[t][m-1][n-1] over t from 0 to k.
Complexity: O(m * n * (k + log(m * n))) time and O(k * m * n) extra space.
from collections import defaultdict
from math import inf
class Solution:
def minCost(self, grid: List[List[int]], k: int) -> int:
m, n = len(grid), len(grid[0])
f = [[[inf] * n for _ in range(m)] for _ in range(k + 1)]
f[0][0][0] = 0
for i in range(m):
for j in range(n):
if i:
f[0][i][j] = min(f[0][i][j], f[0][i - 1][j] + grid[i][j])
if j:
f[0][i][j] = min(f[0][i][j], f[0][i][j - 1] + grid[i][j])
g = defaultdict(list)
for i, row in enumerate(grid):
for j, x in enumerate(row):
g[x].append((i, j))
keys = sorted(g, reverse=True)
for t in range(1, k + 1):
mn = inf
for key in keys:
pos = g[key]
for i, j in pos:
mn = min(mn, f[t - 1][i][j])
for i, j in pos:
f[t][i][j] = mn
for i in range(m):
for j in range(n):
if i:
f[t][i][j] = min(f[t][i][j], f[t][i - 1][j] + grid[i][j])
if j:
f[t][i][j] = min(f[t][i][j], f[t][i][j - 1] + grid[i][j])
return min(f[t][m - 1][n - 1] for t in range(k + 1))