FastPrepMinimum Servers for Cyclic Daily Tasks
Problem · Intervals

Minimum Servers for Cyclic Daily Tasks

Learn this problem
MediumGoogle logoGoogleNEW GRADONSITE INTERVIEW
See Google hiring insights

Problem statement

A daily schedule contains tasks represented as [startMinute, duration]. Each task starts at startMinute minutes after midnight, runs continuously for duration minutes, and repeats at the same time every day. A task may cross midnight.

One server can run at most one task at a time. Intervals are half-open: when one task ends at minute t, the same server may run a task that starts at minute t. Return the minimum number of servers needed to run the repeating schedule.

Function

minDailyServers(tasks: int[][]) → int

Examples

Example 1

tasks = [[1380,60],[0,30],[1380,30]]return = 2

The two tasks starting at 23:00 overlap, so two servers are necessary. The 60-minute task ends exactly at midnight, when the 00:00 task begins, so they may reuse a server.

Example 2

tasks = [[1430,30],[5,20],[30,10]]return = 2

The first task wraps through minute 20 and overlaps the task from minute 5 to 25. The third task starts later.

Example 3

tasks = [[100,1440],[200,10],[210,10]]return = 2

The full-day task is always active. The two shorter half-open tasks do not overlap each other, so one additional server is enough.

Constraints

  • 1 <= tasks.length <= 200000
  • Every task is [startMinute, duration].
  • 0 <= startMinute < 1440.
  • 1 <= duration <= 1440.

More Google problems

drafts saved locally
public int minDailyServers(int[][] tasks) {
    // Write your code here.
}
tasks[[1380,60],[0,30],[1380,30]]
expected2
checking account