Easy Swift solution with O(n) complexity.


  • 0
    S
    func minMoves(_ nums: [Int]) -> Int {
            var min = nums.first!
            var sum = 0
            for num in nums {
                sum += num
                if num < min {
                    min = num
                }
            }
            return sum - ((nums.count-1)*min) - min
        }
    

Log in to reply
 

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