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; + } +};