451. Task Execution Order
Asked in
Task Execution Order

A system has several tasks waiting to be executed. Each task has a unique identifier, a scheduled execution time called runAt, and a priority.

Return the task identifiers in their execution order. Tasks scheduled earlier must execute first. When multiple tasks have the same scheduled time, the task with the higher priority must execute first.

Class

TaskExecutionPlanner

Method

getExecutionOrder

public List<String> getExecutionOrder( List<String> tasks)

  • tasks contains the task configurations.
  • Returns the task identifiers in their required execution order.

Task Format

Every task is represented by a string using the format "taskId,runAt,priority".

  • taskId is the unique identifier of the task.
  • runAt is the scheduled execution time.
  • priority is the task priority. A larger value represents a higher priority.

Execution Rules

  • A task with a smaller runAt value executes first.
  • If two tasks have the same runAt, the task with the larger priority value executes first.
  • If both values are equal, the lexicographically smaller taskId executes first.
  • The supplied tasks list must not be modified.

Constraints

  • 1 ≤ tasks.size() ≤ 100,000
  • Every element uses the exact format "taskId,runAt,priority" without spaces.
  • 1 ≤ taskId.length() ≤ 20
  • Every taskId contains only lowercase English letters.
  • All task identifiers are unique.
  • 0 ≤ runAt ≤ 1,000,000,000
  • 1 ≤ priority ≤ 1,000,000,000
  • runAt and priority are valid integers.
  • tasks and its elements are never null.

Examples

Example 1

getExecutionOrder( tasks = List.of("lint,5,2", "build,2,4", "test,2,8", "deploy,4,7", "docs,5,6"))

Output: List.of("test", "build", "deploy", "docs", "lint")

The tasks scheduled for time 2 execute first, with test before build because it has higher priority. At time 5, docs has higher priority than lint.

Example 2

getExecutionOrder( tasks = List.of("gamma,7,3", "alpha,7,3", "beta,7,9", "setup,1,1"))

Output: List.of("setup", "beta", "alpha", "gamma")

setup has the earliest scheduled time. Among the tasks scheduled for time 7, beta has the highest priority. The remaining tie is resolved lexicographically.

Example 3

getExecutionOrder( tasks = List.of("archive,0,1"))

Output: List.of("archive")

The only supplied task is the first and only task executed.



Please use Laptop/Desktop or any other large screen to add/edit code.