From 4bca527b4b37da21c8c8c0fa30ba9b430aa656bf Mon Sep 17 00:00:00 2001 From: Shafaqun Nisa Date: Sun, 5 Oct 2025 11:04:02 +0530 Subject: [PATCH] 3342. Find Minimum Time to Reach Last Room II.cpp --- ...ind Minimum Time to Reach Last Room II.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 3342. Find Minimum Time to Reach Last Room II.cpp diff --git a/3342. Find Minimum Time to Reach Last Room II.cpp b/3342. Find Minimum Time to Reach Last Room II.cpp new file mode 100644 index 0000000..b0cda75 --- /dev/null +++ b/3342. Find Minimum Time to Reach Last Room II.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + vector>directions{{1,0},{-1,0},{0,1},{0,-1}}; + typedef pair>P; + int minTimeToReach(vector>& moveTime) { + int m=moveTime.size(),n=moveTime[0].size(); + + priority_queue,greater

>pq; + vector>result(m,vector(n,INT_MAX)); + + result[0][0]=0; + pq.push({0,{0,0}}); + + while(!pq.empty()){ + int currTime=pq.top().first; + auto cell=pq.top().second; + + int i=cell.first; + int j=cell.second; + + pq.pop(); + if(i==m-1 && j==n-1) return currTime; + + for(auto &dir:directions){ + int i_=i+dir[0]; + int j_=j+dir[1]; + + if(i_>=0 && i_=0 && j_arrTime){ + result[i_][j_]=arrTime; + pq.push({arrTime,{i_,j_}}); + } + } + } + } + return -1; + } +};