Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

135. Task Scheduler

MediumDay 39 of 60Heap / Priority Queue

A CPU must run a list of tasks, each labelled with a capital letter and taking one unit of time. In each unit the CPU either runs one task or stays idle. Two runs of the same task must be separated by at least n units. Tasks can run in any order. Return the minimum number of units needed to finish all tasks.

Example 1
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
A B idle A B idle A B: each A is 3 units after the previous A.
Example 2
Input: tasks = ["A","C","A","B","D","B"], n = 1
Output: 6
A B A B C D needs no idle time.
Constraints
  • 1 <= tasks.length <= 10^4
  • tasks[i] is an uppercase English letter
  • 0 <= n <= 100
Hints
Frequently asked at: Meta, Amazon, Microsoft
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min