My Simple and Fast Swift Solution


  • 0
    Y
    func minMoves2(_ nums: [Int]) -> Int {
        let sorted = nums.sorted(by: <)
        let middle = nums.count/2
        var ans = 0
        for i in sorted{
            ans += abs(sorted[middle] - i)
        }
        return ans
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.