arr2
.dp
as memory.dfs(i, prev)
to find the minimum number of operations to make arr1[i:]
sorted when arr1[i - 1] = prev
.
(i, prev)
exists in dp
, return dp[(i, prev)]
.arr1[i] > prev
, set cost
to dfs(i+1, arr1[i])
.idx
of the smallest value in arr2
greater than prev
. If idx < len(arr2)
, set cost
to min(cost, 1 + dfs(i+1, arr2[idx]))
.dp[(i, prev)]
with cost
.cost
.dfs(0, -1)
and return its value if it's not float('inf')
, otherwise return -1
.