1102. 得分最高的路径 C++ 优先队列

leetCode:https://leetcode-cn.com/problems/path-with-maximum-minimum-value/

class Cell{
public:
    Cell(int row, int col,int value) : r(row), c(col), val(value)  {}
    bool operator < (const Cell& cell) const {
        return this->val < cell.val;
    }

public:
    int val;
    int r;
    int c;
};
bool last_reach(int i, int j, int r, int c)
{
    return ((i == r - 1) && (j == c - 1)) ? true : false;
}
class Solution {
public:
    int maximumMinimumPath(vector<vector<int>>& A) {
        int row = A.size();
        int col = A[0].size();
        int min = std::min(A[row -1][col - 1], A[0][0]);
        std::priority_queue<Cell> pq;
        vector<vector<int>> visited(row, vector<int>(col, false));
        int i = 0, j = 0;
        visited[0][0] = true;
        pq.push(Cell(0, 0, A[0][0]));
        while (i != row - 1 || j != col -1) {
            visited[i][j] = true;
            if (i - 1 >= 0 && visited[i - 1][j] == 0) {
                visited[i -1][j] = 1;
                pq.push(Cell(i -1, j, A[i -1][j]));
            }
            if (i + 1 < row && visited[i + 1][j] == 0) {
                if (last_reach(i + 1, j, row, col)) return min;
                visited[i + 1][j] = 1;
                pq.push(Cell(i + 1, j, A[i + 1][j]));
            }
            if (j -1 >= 0 && visited[i][j - 1] == 0) {
                visited[i][j -1] = 1;
                pq.push(Cell(i, j - 1, A[i][j - 1]));
            }
            if (j + 1 < col && visited[i][j + 1] == 0) {
                if (last_reach(i , j + 1, row, col)) return min;
                visited[i][j + 1] = 1;
                pq.push(Cell(i, j + 1, A[i][j + 1]));
            }
            Cell nextCell = pq.top();
            if (nextCell.val < min) min = nextCell.val;
            i = nextCell.r;
            j = nextCell.c;
            pq.pop();
        }
        return min;
    }
};



上一篇:LeeCode347前k个高频元素(Java)(最小堆/优先队列)


下一篇:剑指 Offer 40. 最小的k个数