Fewest Terms to Finish All Courses
Updated
Problem statement
There are n courses, numbered from 1 to n. Each entry [before, after] of relations means course before must be finished in an earlier term than course after.
In one term you may take any number of courses, as long as every rule that ends at a course you take has its before course finished in an earlier term.
Return the fewest terms needed to take all n courses, or -1 if it cannot be done.
Function
minimumTerms(n: int, relations: int[][]) → intExamples
Example 1
n = 4relations = [[1,2],[1,3],[3,4]]return = 3Term 1: course 1. Term 2: courses 2 and 3 (both need only course 1). Term 3: course 4 (needs course 3). So 3 terms.
Example 2
n = 5relations = []return = 1No course needs another, so all five can be taken in the first term.
Example 3
n = 4relations = [[1,2],[2,3],[3,2],[1,4]]return = -1Course 2 needs course 3 and course 3 needs course 2, so neither can ever be taken: the answer is -1.
Constraints
1 <= n <= 10000 <= relations.length <= 2000relations[i].length == 2,1 <= relations[i][0], relations[i][1] <= nandrelations[i][0] != relations[i][1]- No pair appears twice in
relations.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleByteDance · PHONE SCREENByteDance interview questions
- Course ScheduleNykaa · PHONE SCREENNykaa interview questions
- Topological Sort for Ads TasksNetflix · PHONE SCREENNetflix interview questions
- Course ScheduleBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Course Schedule FeasibilityTesla · PHONE SCREENTesla interview questions
- Job Schedule with One Initial Ready JobDatadog · PHONE SCREENDatadog interview questions
- Course SchedulePlaud · ONSITE INTERVIEW
- Find a Valid Course Completion OrderGoogle · ONSITE INTERVIEWGoogle interview questions