Swift Solution using stride in the for loop.


  • 0
    D

    It was hard for me to understand what needs to be done, but after several minutes I understood that I have to make the best pairs to sum as much as I can, taking just the minimum of each pair. This is my solution in Swift 3

    class Solution {
        public func arrayPairSum(_ nums: [Int]) -> Int {
            let orderedNums = nums.sorted()
            var sum = 0
            for index in stride(from: 0, to: orderedNums.count, by: 2){
                sum +=  orderedNums[index]
            }
            return sum
        }
    }
    

Log in to reply
 

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