You are given an integer array a of length n. Define one operation as follows:
x = a[0]
.
x
to the end of the array.
x
leftward
while
it has a left neighbor and
x
is
smaller
than that left neighbor (i.e., while
a[i] < a[i-1]
). Stop when either:
x
reaches index
0
, or
a[i-1] <= a[i]
.
After each operation, the array is updated deterministically.
Return the minimum number of operations needed to make the array non-decreasing (i.e., for all i, a[i] <= a[i+1]). If it is impossible for the array to ever become non-decreasing under repeated operations, return -1.
Implement a function like:
min_ops_to_nondecreasing(a: List[int]) -> int
1 <= n <= 2e5
a[i]
fits in 32-bit signed integer