[
sort
simulation
]
BinarySearch 0333 Minimum Cost Sort
Problem statement
https://binarysearch.com/problems/Minimum-Cost-Sort/
Solution
Just do what is asked.
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, nums):
a1 = sum(abs(x - y) for x, y in zip(nums, sorted(nums)))
a2 = sum(abs(x - y) for x, y in zip(nums, sorted(nums)[::-1]))
return min(a1, a2)