Skip to content

Commit bcd6894

Browse files
Question 2050 has been solved.
1 parent 44d950c commit bcd6894

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
4+
5+
vector<vector<int>> graph(n + 1);
6+
for(auto i : relations)
7+
graph[i[1]].push_back(i[0]);
8+
9+
vector<bool> done(n + 1, false);
10+
int answer = 0;
11+
for(int i = 1; i <= n; i++)
12+
if(!done[i])
13+
search(i, graph, time, done, answer);
14+
15+
return answer;
16+
}
17+
18+
int search(int i, vector<vector<int>> &graph, vector<int> &time, vector<bool> &done, int &answer)
19+
{
20+
if(done[i])
21+
return time[i - 1];
22+
23+
done[i] = true;
24+
int temp = 0;
25+
for(auto j : graph[i])
26+
temp = max(temp, search(j, graph, time, done, answer));
27+
28+
time[i - 1] += temp;
29+
answer = max(answer, time[i - 1]);
30+
return time[i - 1];
31+
}
32+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
3+
4+
graph = defaultdict(set)
5+
degree = defaultdict(int)
6+
for i, j in relations:
7+
degree[j-1] += 1
8+
graph[i-1].add(j-1)
9+
10+
count = 0
11+
prev = defaultdict(int)
12+
13+
queue = [(time[x], x) for x in range(n) if degree[x] == 0]
14+
heapify(queue)
15+
16+
while(queue):
17+
temp, course = heappop(queue)
18+
count += 1
19+
if(count == n):
20+
return temp
21+
for nei in graph[course]:
22+
degree[nei] -= 1
23+
prev[nei] = max(prev[nei], temp)
24+
if(degree[nei] == 0):
25+
heappush(queue, (prev[nei] + time[nei], nei))

0 commit comments

Comments
 (0)