diff --git a/3341. Find Minimum Time to Reach Last Room I.cpp b/3341. Find Minimum Time to Reach Last Room I.cpp new file mode 100644 index 0000000..542b8cf --- /dev/null +++ b/3341. Find Minimum Time to Reach Last Room I.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int minTimeToReach(vector>& moveTime) { + int m = moveTime.size(), n = moveTime[0].size(); + + priority_queue>, vector>>, greater<>> pq; + pq.push({0, {0, 0}}); + + vector> dis(m, vector(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; + } +};