mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-15 09:57:09 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c45b01a838 | |||
| cc492aa3b2 |
@@ -0,0 +1,56 @@
|
|||||||
|
class Solution:
|
||||||
|
def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
|
||||||
|
"""
|
||||||
|
Complexities:
|
||||||
|
Time: O(n^2)
|
||||||
|
Space: O(r)
|
||||||
|
|
||||||
|
where n = n, r = len(relations)
|
||||||
|
"""
|
||||||
|
|
||||||
|
# courses been taken array
|
||||||
|
# - O(r) time / O(r) space
|
||||||
|
hasPrereq = [False] * n
|
||||||
|
prereqMap = [set() for i in range(n)]
|
||||||
|
leadsToMap = [set() for i in range(n)]
|
||||||
|
for prevCourse, nextCourse in relations:
|
||||||
|
hasPrereq[nextCourse - 1] = True
|
||||||
|
prereqMap[nextCourse - 1].add(prevCourse)
|
||||||
|
leadsToMap[prevCourse - 1].add(nextCourse)
|
||||||
|
|
||||||
|
# O(n) space
|
||||||
|
timeUntilComplete = [-1] * n
|
||||||
|
coursesCalculated = 0
|
||||||
|
maxOverallTimeFound = 0
|
||||||
|
toResolve = set()
|
||||||
|
|
||||||
|
# calculate timeUntilComplete for no prereq courses
|
||||||
|
# - O(n) time
|
||||||
|
for i in range(n):
|
||||||
|
if not hasPrereq[i]:
|
||||||
|
timeUntilComplete[i] = time[i]
|
||||||
|
maxOverallTimeFound = max(maxOverallTimeFound, time[i])
|
||||||
|
coursesCalculated += 1
|
||||||
|
toResolve.update(leadsToMap[i])
|
||||||
|
|
||||||
|
# find all timeUntilComplete
|
||||||
|
# - if all prereq times are known, then calc
|
||||||
|
# - O(n^2) time
|
||||||
|
while len(toResolve) > 0:
|
||||||
|
nextCourse = toResolve.pop()
|
||||||
|
maxPrereqTime = -1
|
||||||
|
for prereq in prereqMap[nextCourse - 1]:
|
||||||
|
if timeUntilComplete[prereq - 1] == -1:
|
||||||
|
maxPrereqTime = -1
|
||||||
|
break
|
||||||
|
maxPrereqTime = max(maxPrereqTime, timeUntilComplete[prereq - 1])
|
||||||
|
|
||||||
|
if maxPrereqTime > -1:
|
||||||
|
timeUntilComplete[nextCourse - 1] = maxPrereqTime + time[nextCourse - 1]
|
||||||
|
maxOverallTimeFound = max(maxOverallTimeFound, timeUntilComplete[nextCourse - 1])
|
||||||
|
coursesCalculated += 1
|
||||||
|
toResolve.update(leadsToMap[nextCourse - 1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return maxOverallTimeFound
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<h2><a href="https://leetcode.com/problems/parallel-courses-iii">2050. Parallel Courses III</a></h2><h3>Hard</h3><hr><p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevCourse<sub>j</sub></code> has to be completed <strong>before</strong> course <code>nextCourse<sub>j</sub></code> (prerequisite relationship). Furthermore, you are given a <strong>0-indexed</strong> integer array <code>time</code> where <code>time[i]</code> denotes how many <strong>months</strong> it takes to complete the <code>(i+1)<sup>th</sup></code> course.</p>
|
||||||
|
|
||||||
|
<p>You must find the <strong>minimum</strong> number of months needed to complete all the courses following these rules:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>You may start taking a course at <strong>any time</strong> if the prerequisites are met.</li>
|
||||||
|
<li><strong>Any number of courses</strong> can be taken at the <strong>same time</strong>.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>Return <em>the <strong>minimum</strong> number of months needed to complete all the courses</em>.</p>
|
||||||
|
|
||||||
|
<p><strong>Note:</strong> The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong class="example">Example 1:</strong></p>
|
||||||
|
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/10/07/ex1.png" style="width: 392px; height: 232px;" /></strong>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
|
||||||
|
<strong>Output:</strong> 8
|
||||||
|
<strong>Explanation:</strong> The figure above represents the given graph and the time required to complete each course.
|
||||||
|
We start course 1 and course 2 simultaneously at month 0.
|
||||||
|
Course 1 takes 3 months and course 2 takes 2 months to complete respectively.
|
||||||
|
Thus, the earliest time we can start course 3 is at month 3, and the total time required is 3 + 5 = 8 months.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong class="example">Example 2:</strong></p>
|
||||||
|
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/10/07/ex2.png" style="width: 500px; height: 365px;" /></strong>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>Input:</strong> n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
|
||||||
|
<strong>Output:</strong> 12
|
||||||
|
<strong>Explanation:</strong> The figure above represents the given graph and the time required to complete each course.
|
||||||
|
You can start courses 1, 2, and 3 at month 0.
|
||||||
|
You can complete them after 1, 2, and 3 months respectively.
|
||||||
|
Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed after 3 + 4 = 7 months.
|
||||||
|
Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed, i.e., after max(1,2,3,7) = 7 months.
|
||||||
|
Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<p><strong>Constraints:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>0 <= relations.length <= min(n * (n - 1) / 2, 5 * 10<sup>4</sup>)</code></li>
|
||||||
|
<li><code>relations[j].length == 2</code></li>
|
||||||
|
<li><code>1 <= prevCourse<sub>j</sub>, nextCourse<sub>j</sub> <= n</code></li>
|
||||||
|
<li><code>prevCourse<sub>j</sub> != nextCourse<sub>j</sub></code></li>
|
||||||
|
<li>All the pairs <code>[prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> are <strong>unique</strong>.</li>
|
||||||
|
<li><code>time.length == n</code></li>
|
||||||
|
<li><code>1 <= time[i] <= 10<sup>4</sup></code></li>
|
||||||
|
<li>The given graph is a directed acyclic graph.</li>
|
||||||
|
</ul>
|
||||||
Reference in New Issue
Block a user