FunctionRacerProblemsRace

Break Numbers Until Sorted

HardGreedyPython, JavaScript, TypeScript, Java, C++

One move takes a number out of nums and puts two positive whole numbers that add up to it in its place, in either order. For example [5, 7, 3] can become [5, 3, 4, 3].

Return the fewest moves after which the list never goes down from one item to the next.

Example 1

nums [8,5,6]
returns 1

Break 8 into 4 and 4: [4, 4, 5, 6].

Example 2

nums [1,2,3]
returns 0

The list is already in order.

Example 3

nums [7,2]
returns 3

Nothing left of the 2 may be larger than 2, so 7 has to become four pieces such as 1, 2, 2, 2: three moves.

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