eng
competition

Text Practice Mode

Leetcode 1631 in C++

created Yesterday, 17:28 by brlivsky


1


Rating

157 words
4 completed
00:00
using tiii = tuple<int, int, int>;
 
class Solution {
public:
    struct Comporator {
        bool operator() (const tiii &a, const tiii& b) {
            return get<0>(a) > get<0>(b);
        }  
    };
    int minimumEffortPath(vector<vector<int>>& heights) {
        priority_queue<tiii, vector<tiii>, Comporator> pq;           
        int m = heights.size(), n = heights[0].size();
 
        vector<vector<int>> visited(m, vector<int> (n, INT_MAX));
        visited[0][0] = 0;
        pq.push({0, 0, 0});
 
        vector<int> direction = {-1, 0, 1, 0, -1};
        while (!pq.empty()) {
            auto [cur_min_effort, i, j] = pq.top();
            pq.pop();
 
            if (i == m-1 && j == n-1) {
                return cur_min_effort;
            }
 
            for (int idx = 0; idx < 4; idx++) {
                int dx = direction[idx], dy = direction[idx + 1];
 
                int x = i + dx, y = j + dy;
                if (min(x, y) <  || x >= m || y >= n) {
                    continue;
                }
 
                int new_min_effort = max(cur_min_effort, (int) abs(heights[i][j] - heights[x][y]));
                if (new_min_effort >= visited[x][y]) {
                    continue;
                }
 
                visited[x][y] = new_min_effort;
                pq.push({new_min_effort, x, y});
                 
            }
        }
        return 0;
    }
};

saving score / loading statistics ...