order of length n holds each of the numbers 0 to n − 1 exactly once.
The array is cut into several contiguous pieces, each piece is sorted on its own, and the pieces are then glued back together in the same order. The result must be the array sorted in ascending order.
Return the largest number of pieces the array can be cut into this way.order=[2, 0, 1, 4, 3, 5] → 3
Cut it into [2, 0, 1], [4, 3] and [5]: sorting the pieces gives 0 1 2 3 4 5. More pieces are impossible; for instance, 2 cannot be separated from 0 and 1.
order=[0, 1, 2] → 3
The order is already correct, so each element can be its own piece.
order=[3, 1, 2, 0] → 1
The 0 sits at the very end and the 3 at the very start, so they cannot be separated, leaving one piece.
order is a permutation of 0, 1, …, n − 1order=[2, 0, 1, 4, 3, 5]
3
Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.