/**
* Problem: LeetCode 3651 - Minimum Cost Path with Teleportations
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
class Solution {
public int minCost(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
int maxVal = 0;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (grid[r][c] > maxVal) {
maxVal = grid[r][c];
}
}
}
final int INF = 1_000_000_000;
int[][] dp = new int[m][n];
for (int r = 0; r < m; r++) {
Arrays.fill(dp[r], INF);
}
// Base case: starting at (0, 0) costs 0
dp[0][0] = 0;
// Base layer (0 teleportations): normal moves only
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (r > 0 && dp[r - 1][c] != INF) {
dp[r][c] = Math.min(dp[r][c], dp[r - 1][c] + grid[r][c]);
}
if (c > 0 && dp[r][c - 1] != INF) {
dp[r][c] = Math.min(dp[r][c], dp[r][c - 1] + grid[r][c]);
}
}
}
// Up to k teleportations
int[] minAtVal = new int[maxVal + 1];
int[] suffMin = new int[maxVal + 1];
for (int t = 0; t < k; t++) {
Arrays.fill(minAtVal, INF);
// Record minimum cost reachable at each cell value
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
int val = grid[r][c];
if (dp[r][c] < minAtVal[val]) {
minAtVal[val] = dp[r][c];
}
}
}
// suffMin[v] = min cost to reach any cell with value >= v
int curMin = INF;
for (int v = maxVal; v >= 0; v--) {
if (minAtVal[v] < curMin) {
curMin = minAtVal[v];
}
suffMin[v] = curMin;
}
int[][] nextDp = new int[m][n];
// Teleportation step (cost 0): to cell (r, c), we can teleport from any cell with value >= grid[r][c]
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
nextDp[r][c] = Math.min(dp[r][c], suffMin[grid[r][c]]);
}
}
// Normal moves step after teleportation
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (r > 0 && nextDp[r - 1][c] != INF) {
nextDp[r][c] = Math.min(nextDp[r][c], nextDp[r - 1][c] + grid[r][c]);
}
if (c > 0 && nextDp[r][c - 1] != INF) {
nextDp[r][c] = Math.min(nextDp[r][c], nextDp[r][c - 1] + grid[r][c]);
}
}
}
dp = nextDp;
}
return dp[m - 1][n - 1];
}
}
"""
Problem: LeetCode 3651 - Minimum Cost Path with Teleportations
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def minCost(self, grid: List[List[int]], k: int) -> int:
m, n = len(grid), len(grid[0])
max_val = max(max(row) for row in grid)
INF = float('inf')
dp = [[INF] * n for _ in range(m)]
# Base case: starting at (0, 0) costs 0
dp[0][0] = 0
# Base layer (0 teleportations): normal moves only
for r in range(m):
for c in range(n):
if r > 0 and dp[r - 1][c] != INF:
dp[r][c] = min(dp[r][c], dp[r - 1][c] + grid[r][c])
if c > 0 and dp[r][c - 1] != INF:
dp[r][c] = min(dp[r][c], dp[r][c - 1] + grid[r][c])
# Up to k teleportations
for _ in range(k):
min_at_val = [INF] * (max_val + 1)
for r in range(m):
for c in range(n):
val = grid[r][c]
if dp[r][c] < min_at_val[val]:
min_at_val[val] = dp[r][c]
# suff_min[v] = min cost to reach any cell with value >= v
suff_min = [INF] * (max_val + 1)
cur_min = INF
for v in range(max_val, -1, -1):
if min_at_val[v] < cur_min:
cur_min = min_at_val[v]
suff_min[v] = cur_min
next_dp = [[INF] * n for _ in range(m)]
# Teleportation step (cost 0)
for r in range(m):
for c in range(n):
next_dp[r][c] = min(dp[r][c], suff_min[grid[r][c]])
# Normal moves step after teleportation
for r in range(m):
for c in range(n):
if r > 0 and next_dp[r - 1][c] != INF:
next_dp[r][c] = min(next_dp[r][c], next_dp[r - 1][c] + grid[r][c])
if c > 0 and next_dp[r][c - 1] != INF:
next_dp[r][c] = min(next_dp[r][c], next_dp[r][c - 1] + grid[r][c])
dp = next_dp
return dp[m - 1][n - 1]
/**
* Problem: LeetCode 3651 - Minimum Cost Path with Teleportations
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Solution {
public:
int minCost(vector<vector<int>>& grid, int k) {
int m = grid.size();
int n = grid[0].size();
int maxVal = 0;
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
maxVal = max(maxVal, grid[r][c]);
}
}
const int INF = 1e9;
vector<vector<int>> dp(m, vector<int>(n, INF));
// Base case: starting at (0, 0) costs 0
dp[0][0] = 0;
// Base layer (0 teleportations): normal moves only
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
if (r > 0 && dp[r - 1][c] != INF) {
dp[r][c] = min(dp[r][c], dp[r - 1][c] + grid[r][c]);
}
if (c > 0 && dp[r][c - 1] != INF) {
dp[r][c] = min(dp[r][c], dp[r][c - 1] + grid[r][c]);
}
}
}
vector<int> minAtVal(maxVal + 1);
vector<int> suffMin(maxVal + 1);
// Up to k teleportations
for (int t = 0; t < k; ++t) {
fill(minAtVal.begin(), minAtVal.end(), INF);
// Record minimum cost reachable at each cell value
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
int val = grid[r][c];
minAtVal[val] = min(minAtVal[val], dp[r][c]);
}
}
// suffMin[v] = min cost to reach any cell with value >= v
int curMin = INF;
for (int v = maxVal; v >= 0; --v) {
curMin = min(curMin, minAtVal[v]);
suffMin[v] = curMin;
}
vector<vector<int>> nextDp(m, vector<int>(n, INF));
// Teleportation step (cost 0)
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
nextDp[r][c] = min(dp[r][c], suffMin[grid[r][c]]);
}
}
// Normal moves step after teleportation
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
if (r > 0 && nextDp[r - 1][c] != INF) {
nextDp[r][c] = min(nextDp[r][c], nextDp[r - 1][c] + grid[r][c]);
}
if (c > 0 && nextDp[r][c - 1] != INF) {
nextDp[r][c] = min(nextDp[r][c], nextDp[r][c - 1] + grid[r][c]);
}
}
}
dp = move(nextDp);
}
return dp[m - 1][n - 1];
}
};
/**
* Problem: LeetCode 3651 - Minimum Cost Path with Teleportations
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[][]} grid
* @param {number} k
* @return {number}
*/
var minCost = function(grid, k) {
const m = grid.length;
const n = grid[0].length;
let maxVal = 0;
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
if (grid[r][c] > maxVal) {
maxVal = grid[r][c];
}
}
}
const INF = 1e15;
let dp = Array.from({ length: m }, () => Array(n).fill(INF));
// Base case: starting at (0, 0) costs 0
dp[0][0] = 0;
// Base layer (0 teleportations): normal moves only
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
if (r > 0 && dp[r - 1][c] !== INF) {
dp[r][c] = Math.min(dp[r][c], dp[r - 1][c] + grid[r][c]);
}
if (c > 0 && dp[r][c - 1] !== INF) {
dp[r][c] = Math.min(dp[r][c], dp[r][c - 1] + grid[r][c]);
}
}
}
// Up to k teleportations
for (let t = 0; t < k; t++) {
const minAtVal = Array(maxVal + 1).fill(INF);
// Record minimum cost reachable at each cell value
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
const val = grid[r][c];
if (dp[r][c] < minAtVal[val]) {
minAtVal[val] = dp[r][c];
}
}
}
// suffMin[v] = min cost to reach any cell with value >= v
const suffMin = Array(maxVal + 1).fill(INF);
let curMin = INF;
for (let v = maxVal; v >= 0; v--) {
if (minAtVal[v] < curMin) {
curMin = minAtVal[v];
}
suffMin[v] = curMin;
}
const nextDp = Array.from({ length: m }, () => Array(n).fill(INF));
// Teleportation step (cost 0)
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
nextDp[r][c] = Math.min(dp[r][c], suffMin[grid[r][c]]);
}
}
// Normal moves step after teleportation
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
if (r > 0 && nextDp[r - 1][c] !== INF) {
nextDp[r][c] = Math.min(nextDp[r][c], nextDp[r - 1][c] + grid[r][c]);
}
if (c > 0 && nextDp[r][c - 1] !== INF) {
nextDp[r][c] = Math.min(nextDp[r][c], nextDp[r][c - 1] + grid[r][c]);
}
}
}
dp = nextDp;
}
return dp[m - 1][n - 1];
};