Created
April 11, 2020 20:39
-
-
Save piyush23dez/29f914f2636dd11fe4f59571e88afd2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func connectSticks(_ sticks: [Int]) -> Int { | |
let sticks = sticks.sorted() | |
var connSticks = [Int]() | |
var stick = sticks[0]+sticks[1] | |
connSticks.append(stick) | |
for i in 2..<sticks.count { | |
stick = stick + sticks[i] | |
connSticks.append(stick) | |
} | |
let res = connSticks.reduce(0, +) | |
return res | |
} | |
let arr = [2,4,3] | |
let val = connectSticks(arr) | |
print(val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment