Loading...
Loading...
You are building a simplified task execution engine that models both synchronous and asynchronous task pipelines. Each task has an ID, an execution duration, and a list of dependencies (tasks that must complete before it can start).
Your job is to simulate two execution modes:
S): Tasks are executed one at a time in topological order. Each task starts only after the previous one finishes, regardless of dependencies being satisfied earlier.A): Tasks are executed as soon as all their dependencies are complete (parallel execution is allowed). Tasks with no unmet dependencies can run simultaneously.Given a list of tasks with durations and dependencies, and a mode, return the total time to complete all tasks.
n — number of tasks (0-indexed, IDs from 0 to n-1)durations — integer array where durations[i] is the time task i takesdependencies — list of [a, b] pairs meaning task b must finish before task a startsmode — string "S" for synchronous, "A" for asynchronousReturn a single integer — the total completion time.
Input: n=3, durations=[3,2,5], dependencies=[], mode="S"
Output: 10
Explanation: Synchronous means tasks run one after another. Total = 3 + 2 + 5 = 10.
Input: n=3, durations=[3,2,5], dependencies=[], mode="A"
Output: 5
Explanation: With no dependencies, all tasks run in parallel. Total time = max(3, 2, 5) = 5.
Input: n=3, durations=[3,2,5], dependencies=[[1,0],[2,1]], mode="A"
Output: 10
Explanation: Task 0 starts at t=0, finishes at t=3. Task 1 can start at t=3, finishes at t=5. Task 2 can start at t=5, finishes at t=10. Even in async mode, the dependency chain forces sequential execution here.
1 <= n <= 10^4 1 <= durations[i] <= 10^3 0 <= dependencies.length <= 10^4 dependencies[a][0] != dependencies[a][1] No cyclic dependencies guaranteed mode is either "S" or "A"
max(finish times of all its dependencies)max(start[i] + duration[i])