↓Skip to main content
  1. LeetCode/

LeetCode 3651: Minimum Cost Path with Teleportations

·2 mins· ·
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-06-26

  • Submissions: Accepted ×3

Approach
#

dp_t[r][c] = the least cost to reach (r, c) with at most t teleports.

  • t = 0: the usual right / down grid DP.
  • From layer t - 1 to layer t:
    1. Teleport: from any cell with value >= grid[r][c], at cost 0. So the starting cost at (r, c) = the previous layer’s minimum dp over all cells with value >= grid[r][c]. Sort cells by value descending and keep a running minimum.
    2. After teleporting you can still move right / down, so run the grid DP again.

Every layer updates the answer at the destination, for up to k layers.

My Solution
#

class Solution:
    def minCost(self, grid: List[List[int]], k: int) -> int:
        m, n = len(grid), len(grid[0])
        INF = float("inf")
        cells = sorted(((grid[r][c], r, c) for r in range(m) for c in range(n)), reverse=True)

        def walk(dp: List[List[float]]) -> None:     # the usual right / down DP
            for r in range(m):
                for c in range(n):
                    if r > 0:
                        dp[r][c] = min(dp[r][c], dp[r - 1][c] + grid[r][c])
                    if c > 0:
                        dp[r][c] = min(dp[r][c], dp[r][c - 1] + grid[r][c])

        dp = [[INF] * n for _ in range(m)]
        dp[0][0] = 0
        walk(dp)
        ans = dp[m - 1][n - 1]

        for _ in range(k):
            best_from = {}                           # value -> previous layer's min over values >= it
            running = INF
            for val, r, c in cells:                  # largest first
                running = min(running, dp[r][c])
                best_from[val] = running
            dp = [[best_from[grid[r][c]] for c in range(n)] for r in range(m)]
            walk(dp)
            ans = min(ans, dp[m - 1][n - 1])
        return ans

Equal values appear consecutively, so best_from[val] ends up overwritten with the minimum including every cell of that value, letting equal-valued cells teleport to each other too.

Complexity
#

Time O(mn log(mn) + k · mn), space O(mn).

Mistakes to Avoid
#

  • The teleport direction: from larger values to smaller, so reaching (r, c) looks at cells with value >= it.
  • Run the normal DP again after teleporting, since you can keep walking.

Interview Explanation
#

I compute one layer per teleport. Each layer first handles teleports: the cost into a cell is the previous layer’s minimum over every cell with value at least its own, computed in one pass by sorting cells by value descending with a running minimum; then I rerun the right / down grid DP. Each layer updates the destination’s answer. Time O(mn log(mn) + k · mn).

Related

LeetCode 312: Burst Balloons
·2 mins
LeetCode Hard Dynamic-Programming Interval-Dp Array
LeetCode 312 note: choose the last balloon k burst in the range (i, j); at that moment its neighbors are i and j, and the two sides are independent subproblems
LeetCode 1537: Get the Maximum Score
·3 mins
LeetCode Hard Dynamic-Programming Two-Pointers Greedy Array
LeetCode 1537 note: two sorted arrays can switch only at shared values; merge with two pointers, keep a running sum per path, and at a shared value set both to the larger sum plus that value
LeetCode 1458: Max Dot Product of Two Subsequences
·2 mins
LeetCode Hard Dynamic-Programming Longest-Common-Subsequence Array
LeetCode 1458 note: a two-prefix DP; dp[i][j] either pairs nums1[i] with nums2[j] (extending dp[i-1][j-1] or starting fresh) or skips one of them
LeetCode 42: Trapping Rain Water
·2 mins
LeetCode Hard Array Two-Pointers Dynamic-Programming Stack
LeetCode 42 note: each cell holds min(tallest on the left, tallest on the right) - its own height; use prefix and suffix maxima or two pointers
LeetCode 84: Largest Rectangle in Histogram
·3 mins
LeetCode Daily Hard Array Stack Monotonic-Stack Dynamic-Programming Geometry Histogram
Solving the Largest Rectangle in Histogram problem using monotonic stack approach
LeetCode 3660: Jump Game IX
·2 mins
LeetCode Medium Dynamic-Programming Array
LeetCode 3660 note: cutting between i and i+1, if the left max exceeds the right min the two sides reach each other and share the answer; otherwise the left answer is the left max