Minimum Time to Complete All Tasks

There is a computer that can run an unlimited number of tasks at the same time. You are given a 2D integer array tasks where tasks[i] = [starti, endi, durationi] indicates that the i^th task should run for a total of durationi seconds, not necessarily continuously, within the inclusive time range [starti, endi].

You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.

Return the minimum time during which the computer should be turned on to complete all tasks.

Example 1
Inputtasks = [[2,3,1],[4,5,1],[1,5,2]]
Output2
The first task can run at time 2, the second at time 5, and the third at times 2 and 5, so the computer is on for 2 seconds total.
Example 2
Inputtasks = [[1,3,2],[2,5,3],[5,6,2]]
Output4
The tasks can be completed by running at times 2, 3, 5, and 6, so the computer is on for 4 seconds total.

Constraints

  • 1 <= tasks.length <= 2000
  • tasks[i].length == 3
  • 1 <= starti, endi <= 2000
  • 1 <= durationi <= endi - starti + 1

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate