Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Task Scheduler

Medium
heapgreedycounting

A CPU runs tasks labeled A–Z, one per time unit, and identical tasks must be at least n units apart (idle slots allowed). Return the minimum total units to finish all tasks.

Input

  • Line 1: the task letters as one string (e.g. AAB)
  • Line 2: integer n (the cooldown)

Output: the minimum number of time units.

Example 1
Input:
AAABBB
2
Output:
8

A B idle A B idle A B.

Example 2
Input:
AAABBB
0
Output:
6
Example 3
Input:
AAAABBBBCCD
2
Output:
11

A B C A B C A B D A B — no idles needed.

Constraints

  • 1 ≤ tasks ≤ 10^4
  • 0 ≤ n ≤ 100
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.