We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Yeah, quite intutive but very few scalable. You're "lucky" the amount of values to take out from the total is just one. Otherwise you would have an ordered list of values filled as you iterate through the array, requiring O(nlogn) time. Hence I think the most scalable solution required O(nlogn) time, i.e. sorting the array in ascending order and taking the first n elements for the min sum and the last n for the max sum.
This is my code in swift, if it can help anyone:
importFoundation// Enter your code here letinputNumbers=readLine()!.components(separatedBy:" ").map{Int($0)!}letorderedNumbers=inputNumbers.sorted(by:<)letnumbersToSum=4varmin:UInt64=0varmax:UInt64=0(0..<numbersToSum).forEach{letnextMinValue=orderedNumbers[$0]letnextMaxValue=orderedNumbers[orderedNumbers.count-($0+1)]min+=UInt64(exactly:nextMinValue)!max+=UInt64(exactly:nextMaxValue)!}print("\(min) \(max)")
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
Yeah, quite intutive but very few scalable. You're "lucky" the amount of values to take out from the total is just one. Otherwise you would have an ordered list of values filled as you iterate through the array, requiring O(nlogn) time. Hence I think the most scalable solution required O(nlogn) time, i.e. sorting the array in ascending order and taking the first n elements for the min sum and the last n for the max sum.
This is my code in swift, if it can help anyone: