Minimum Cost Sort
Given a list of integers nums
, return the minimum cost of sorting the list in ascending or descending order. The cost is defined as the sum of absolute differences between any element’s old and new value.
Constraints
n ≤ 100,000
wheren
is the length ofnums
https://binarysearch.com/problems/Minimum-Cost-Sort
Examples
Example 1
Input
- nums =
[1, 4, 3]
Output
- answer =
2
Explanation
The cost to change the list to ascending order is 2
:
- Change
4
to3
for a cost of1
- Change
3
to4
for a cost of1
Example 2
Input
- nums =
[7, 3, 5]
Output
- answer =
4
Explanation
The cost to change the list to descending order is 4
:
- Change
3
to5
for a cost of2
- Change
5
to3
for a cost of2
Leave a comment