FunctionRacerProblemsRace

Fewest Rounds of Tasks

EasyGreedyPython, JavaScript, TypeScript, Java, C++

levels[i] is the difficulty level of task i. In one round you complete either 2 or 3 tasks, and all the tasks completed in a round must have the same level.

Return the fewest rounds needed to complete every task, or -1 if it is impossible, which happens exactly when some level occurs only once. The fewest number of rounds is unique, so no tie rule is needed. Return 0 when levels is empty.

Example 1

levels [2,2,3,3,2,4,4,4,4,4]
returns 4

Level 2 occurs 3 times (one round), level 3 twice (one round) and level 4 five times (a round of 3 and a round of 2): 4 rounds.

Example 2

levels [2,3,3]
returns -1

Level 2 occurs only once, so it can never be part of a round of 2 or 3.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems