Skip to content

Commit cf601f4

Browse files
author
rom.spiridonov
committed
feat: add minTime solutions in Python, C++, and Go for LC problem #3604
1 parent 03892a9 commit cf601f4

File tree

1 file changed

+16
-15
lines changed
  • solution/3600-3699/3604.Minimum Time to Reach Destination in Directed Graph

1 file changed

+16
-15
lines changed

solution/3600-3699/3604.Minimum Time to Reach Destination in Directed Graph/Solution.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
class Solution {
22
vector<vector<vector<int>>> adj;
33
vector<int> sol;
4-
priority_queue<pair<int,int> ,vector<pair<int,int>>,greater<>> pq;
5-
void pushNeighbours(int node,int curr){
6-
for(auto it : adj[node]){
7-
int temp = it[0] , start = it[1],end = it[2],newTime = curr+1;
8-
if(curr<start) newTime = start+1;
9-
if(newTime < sol[temp] && newTime-1<=end){
10-
pq.push({newTime,temp});
4+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
5+
void pushNeighbours(int node, int curr) {
6+
for (auto it : adj[node]) {
7+
int temp = it[0], start = it[1], end = it[2], newTime = curr + 1;
8+
if (curr < start) newTime = start + 1;
9+
if (newTime < sol[temp] && newTime - 1 <= end) {
10+
pq.push({newTime, temp});
1111
sol[temp] = newTime;
1212
}
1313
}
1414
}
15+
1516
public:
1617
int minTime(int n, vector<vector<int>>& edges) {
1718
adj = vector<vector<vector<int>>>(n);
18-
for(auto it: edges)
19-
adj[it[0]].push_back({it[1],it[2],it[3]});
20-
sol = vector<int> (n,INT_MAX);
21-
sol[0]=0;
22-
for(pq.push({0,0});!pq.empty();pq.pop())
23-
pushNeighbours(pq.top().second,pq.top().first);
24-
if(sol[n-1] == INT_MAX) return -1;
25-
return sol[n-1];
19+
for (auto it : edges)
20+
adj[it[0]].push_back({it[1], it[2], it[3]});
21+
sol = vector<int>(n, INT_MAX);
22+
sol[0] = 0;
23+
for (pq.push({0, 0}); !pq.empty(); pq.pop())
24+
pushNeighbours(pq.top().second, pq.top().first);
25+
if (sol[n - 1] == INT_MAX) return -1;
26+
return sol[n - 1];
2627
}
2728
};
2829
const auto __ = []() {

0 commit comments

Comments
 (0)