Most Pieces That Sort
Cut nums into pieces, each a stretch of neighbors, with every item in exactly one piece. Sorting each piece on its own and leaving the pieces where they are must give the whole list sorted. Return the largest number of pieces possible.
Example 1
nums [2,1,3,4,4]
returns 4
The pieces [2, 1], [3], [4], [4].
Example 2
nums [5,4,3,2,1]
returns 1
The 5 has to travel to the far end, so nothing can be cut off.
Constraints
1 <= len(nums) <= 1000 <= nums[i] <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.