LeetCode 3651 | Minimum Cost Path with Teleportations | Java | Hard | D P | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 3651 - Minimum Cost Path with Teleportations using Dynamic Programming and Suffix Minimum Optimization in Java.
We are given an m x n grid and an integer k, starting at the top-left cell (0, 0) with 0 initial cost to reach the bottom-right cell (m - 1, n - 1). Normal moves can only proceed right or down at the cost of the destination cell, which forms a natural Directed Acyclic Graph (DAG). Additionally, we can make up to k free teleportations from any cell (i, j) to any cell (x, y) where the value of grid[x][y] is less than or equal to the value of grid[i][j]. Because teleportation cost is zero and the condition is transitive, consecutive teleports never outperform a single teleport, allowing us to model the entire process as alternating rounds of teleports and DAG relaxations.
We formulate an optimal layered dynamic programming approach where each layer represents using at most t teleports, from 0 to k. In each layer, transitioning into a teleported cell depends solely on finding the minimal cost among cells whose grid value is greater than or equal to the current cell value. We optimize this transition in O(maxVal) time using a suffix minimum array over all cell values up to 10,000, followed by standard 2D grid DP relaxation in O(m * n) time. This brings our total execution time down to just a few milliseconds.
π₯ Problem Highlights
β Normal moves proceed only right and down, forming a cycle-free DAG
β Free teleportation allows jumping to any cell with a value less than or equal to the current cell value
β Transitivity proves consecutive teleportations can be collapsed into one jump
β Layered DP cleanly decouples teleportation allowance into k rounds
β Suffix minimum array over grid values eliminates the need for an O(m * n) inner search
β Time Complexity: O(k * (m * n + maxVal)) | Auxiliary Space: O(m * n + maxVal)
β±οΈ TIMESTAMPS
00:00 Intro
00:48 Problem Understanding
01:55 Key Observation
03:18 DP and Suffix Minimum Approach
04:45 Java Code Walkthrough
06:01 Execution and Test Cases
07:09 Time Complexity Analysis
08:05 Space Complexity Analysis
08:42 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Hard Problems
Java DSA Preparation
Layered Dynamic Programming
Grid Shortest Path
Suffix Minimum Optimization
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why can two consecutive teleportations without intermediate normal moves always be replaced by a single teleportation?
1οΈβ£ The teleport counter resets after every second move
2οΈβ£ The condition that the destination cell value is less than or equal to the source cell value is transitive, so a direct jump to the second target is already valid
3οΈβ£ Teleportation cost increases exponentially with consecutive moves
4οΈβ£ Teleporting twice in a row causes an infinite loop in the DP state
#LeetCode3651 #LeetCode #MinimumCostPathWithTeleportations #Java #JavaDSA #DynamicProgramming #Matrix #Array #DSA #CodingInterview #ProblemSolving #LeetCodeHard #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 3651 - Minimum Cost Path with Teleportations using Dynamic Programming and Suffix Minimum Optimization in Java.
We are given an m x n grid and an integer k, starting at the top-left cell (0, 0) with 0 initial cost to reach the bottom-right cell (m - 1, n - 1). Normal moves can only proceed right or down at the cost of the destination cell, which forms a natural Directed Acyclic Graph (DAG). Additionally, we can make up to k free teleportations from any cell (i, j) to any cell (x, y) where the value of grid[x][y] is less than or equal to the value of grid[i][j]. Because teleportation cost is zero and the condition is transitive, consecutive teleports never outperform a single teleport, allowing us to model the entire process as alternating rounds of teleports and DAG relaxations.
We formulate an optimal layered dynamic programming approach where each layer represents using at most t teleports, from 0 to k. In each layer, transitioning into a teleported cell depends solely on finding the minimal cost among cells whose grid value is greater than or equal to the current cell value. We optimize this transition in O(maxVal) time using a suffix minimum array over all cell values up to 10,000, followed by standard 2D grid DP relaxation in O(m * n) time. This brings our total execution time down to just a few milliseconds.
π₯ Problem Highlights
β Normal moves proceed only right and down, forming a cycle-free DAG
β Free teleportation allows jumping to any cell with a value less than or equal to the current cell value
β Transitivity proves consecutive teleportations can be collapsed into one jump
β Layered DP cleanly decouples teleportation allowance into k rounds
β Suffix minimum array over grid values eliminates the need for an O(m * n) inner search
β Time Complexity: O(k * (m * n + maxVal)) | Auxiliary Space: O(m * n + maxVal)
β±οΈ TIMESTAMPS
00:00 Intro
00:48 Problem Understanding
01:55 Key Observation
03:18 DP and Suffix Minimum Approach
04:45 Java Code Walkthrough
06:01 Execution and Test Cases
07:09 Time Complexity Analysis
08:05 Space Complexity Analysis
08:42 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/28-January/3651-Minimum-Cost-Path-with-Teleportations/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Hard Problems
Java DSA Preparation
Layered Dynamic Programming
Grid Shortest Path
Suffix Minimum Optimization
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why can two consecutive teleportations without intermediate normal moves always be replaced by a single teleportation?
1οΈβ£ The teleport counter resets after every second move
2οΈβ£ The condition that the destination cell value is less than or equal to the source cell value is transitive, so a direct jump to the second target is already valid
3οΈβ£ Teleportation cost increases exponentially with consecutive moves
4οΈβ£ Teleporting twice in a row causes an infinite loop in the DP state
#LeetCode3651 #LeetCode #MinimumCostPathWithTeleportations #Java #JavaDSA #DynamicProgramming #Matrix #Array #DSA #CodingInterview #ProblemSolving #LeetCodeHard #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




