heights. The teacher wants heights to never decrease from left to right, but will only rearrange people inside **one contiguous stretch** of the line: that stretch gets sorted in ascending order and everyone else stays put.
Return the length of the shortest such stretch that makes the whole line ordered. If the line is already in order, return 0.
Sorting in O(n log n) is the obvious route; try to do it in O(n).heights=[150, 158, 155, 162, 170, 165, 180] → 5
Reordering positions 1 to 5, [158, 155, 162, 170, 165] → [155, 158, 162, 165, 170], fixes the whole line. The length is 5.
heights=[3, 3, 4, 9] → 0
The line is already in order, so nobody has to move.
heights=[5, 1] → 2
Both people have to be reordered.
heights=[150, 158, 155, 162, 170, 165, 180]
5
Run checks the first 3 tests in your browser. Submit checks all 161 tests on the server, hidden ones included.