FunctionRacerProblemsRace

Most Pieces to Sort Separately

MediumStacks and queuesPython, JavaScript, TypeScript, Java, C++

Cut nums into pieces of consecutive items so that sorting every piece on its own, while the pieces stay in place, makes the whole list sorted from smallest to largest. Return the largest number of pieces possible. An empty list has 0 pieces.

Example 1

nums [2,1,3,4,4]
returns 4

The pieces [2,1], [3], [4] and [4] work.

Example 2

nums [5,4,3,2,1]
returns 1

Every item is out of place, so the only choice is one piece.

Example 3

nums [1,1,0,2]
returns 2

The 0 must move in front of both 1s, so they share a piece, and the 2 can be its own piece.

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 Stacks and queues problems