Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 3341. Find Minimum Time to Reach Last Room I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
int minTimeToReach(vector<vector<int>>& moveTime) {
int m = moveTime.size(), n = moveTime[0].size();

priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>> pq;
pq.push({0, {0, 0}});

vector<vector<int>> dis(m, vector<int>(n, INT_MAX));
dis[0][0] = 0;

int dir[5] = {-1, 0, 1, 0, -1};

while (!pq.empty()) {
auto [curr, pos] = pq.top(); pq.pop();
int row = pos.first, col = pos.second;

if (row == m - 1 && col == n - 1)
return curr;

for (int i = 0; i < 4; i++) {
int nrow = row + dir[i], ncol = col + dir[i + 1];
if (nrow >= 0 && ncol >= 0 && nrow < m && ncol < n) {
int nextTime = max(curr, moveTime[nrow][ncol]) + 1;
if (nextTime < dis[nrow][ncol]) {
dis[nrow][ncol] = nextTime;
pq.push({nextTime, {nrow, ncol}});
}
}
}
}

return -1;
}
};