The problem. A CPU must run a list of tasks, each a capital letter. Each task takes one unit of time, and in each unit the CPU either runs one task or sits idle. Two copies of the same task must be at least n units apart — there must be n other units between them. Tasks can run in any order. Return the least number of units needed to finish them all.
tasks = [A, A, A, B, B, B], n = 2 -> 8 A B idle A B idle A B
tasks = [A, C, A, B, D, B], n = 1 -> 6 A B A B C D (no idling needed)
tasks = [A, A, A, B, B, B], n = 3 -> 10 A B idle idle A B idle idle A BThe task with the most copies is the bottleneck: its copies must be spread out, and everything else fills the gaps between them. So at every unit, the sensible choice is to run the available task with the most copies left. (Running a task with few copies left early can't help — it can only use up a filler you'd need later.)
Free account
Sign up to read the rest of this lesson: 7 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come