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,000wherenis 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
4to3for a cost of1 - Change
3to4for 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
3to5for a cost of2 - Change
5to3for a cost of2
Leave a comment