Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
To find the minimum cost to make nums non-decreasing, use a max heap that contains all of the elements that you have seen so far.
If the largest number in the heap is greater than the current number, calculate the cost to make the two numbers equal. Then, in the heap, replace the largest number with the current number.
Now that we have found the minimum cost to make nums non-decreasing, we can set every nums[i] to -nums[i] and do the same process to find the minimum cost to make nums non-increasing.