Approach

This is a really hard problem! the idea is easy but there are many case to consider, and also a second part for this problem is to do efficient teleporatoin computation.

This is a dp like problme, we can think that when there are no teleport, we can calcualte the answer simply by checking each grid’s top and left. Lets call this answer matrix t0.

When doing one teleportation, I was thinking that we can do teleportation from t0, but actually, we can also walk after the teleportation!! Therefore, we need to update t1 first based on t0, and then do the top/left traversal for the t1 again to update the optimal value.

Another complex part is the teleport part, where I was originally using 4 for-loops which is extremly slow. Actually, teleportation would not care the actual location, so we can sort them based on the grid value first. And we get a cluster of same grid value, and update the best cost based on previous and current grid value, and then we update this valud to all grid that have the same value, and repeating to do this. This can be done in O(NlgN), which is much faster then O(N^2), where N=m*n.

Code

class Solution {
public:
    int minCost(vector<vector<int>>& grid, int k) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>> cost(n, vector<int>(m, INT_MAX));
        cost[0][0] = 0;
        for(int i = 0 ; i < n ; ++i) {
            for(int j = 0 ; j < m ; ++j) {
                if(i == 0 && j == 0) continue;
                int top = i > 0 ? cost[i-1][j] : INT_MAX;
                int left = j > 0 ? cost[i][j-1] : INT_MAX;
                cost[i][j] = min(left, top) + grid[i][j];
            }
        }

        for(int t = 0 ; t < k ; ++t) {
            // print(cost);
            vector<vector<int>> cost_next = cost;
            // for(int i = 0 ; i < n ; ++i) {
            //     for(int j = 0 ; j < m ; ++j) { // current grid i, j
            //         for(int x = 0 ; x < n ; ++x) {
            //             for(int y = 0 ; y < m ; ++y) {
            //                 if(grid[x][y] < grid[i][j]) continue;
            //                 cost_next[i][j] = min(cost_next[i][j], cost[x][y]);
            //             }
            //         }
            //     }
            // }        
            vector<pair<int,int>> cells; // (grid value, index)
            cells.reserve(n * m);
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    cells.emplace_back(grid[i][j], i * m + j);
                }
            }
            sort(cells.begin(), cells.end(), greater<>());

            int best = INT_MAX;
            int idx = 0;
            while (idx < cells.size()) {
                int val = cells[idx].first;

                // same value 
                int start = idx;
                while (idx < cells.size() && cells[idx].first == val) idx++;

                for (int t = start; t < idx; ++t) {
                    int id = cells[t].second;
                    int x = id / m;
                    int y = id % m;
                    best = min(best, cost[x][y]);
                }

                for (int t = start; t < idx; ++t) {
                    int id = cells[t].second;
                    int x = id / m;
                    int y = id % m;
                    if (best != INT_MAX)
                        cost_next[x][y] = min(cost_next[x][y], best);
                }

            }

            for(int i = 0 ; i < n ; ++i) {
                for(int j = 0 ; j < m ; ++j) {
                    if(i == 0 && j == 0) continue;
                    int top = i > 0 ? cost_next[i-1][j] : INT_MAX;
                    int left = j > 0 ? cost_next[i][j-1] : INT_MAX;
                    cost_next[i][j] = min(cost_next[i][j], min(left, top) + grid[i][j]);
                }
            }
            cost = cost_next;
        }
        return cost[n-1][m-1];
    }


    void print(const vector<vector<int>>& v) {
        for(int i = 0 ; i < v.size() ; ++i) {
            for(int j = 0 ; j < v[0].size() ; ++j) {
                cout << v[i][j] << " ";
            }
            cout << endl;
        }
        cout << "---" << endl;
    }
};