Leetcode Note: Go - Minimum Sum of Four Digit Number After Splitting Digits

Minimum Sum of Four Digit Number After Splitting Digits - LeetCode
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/

  • Go 言語で取り組んだメモ

回答

Golang Solution with explanation - Minimum Sum of Four Digit Number After Splitting Digits - LeetCode
https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/solutions/1750426/golang-solution-with-explanation/

func minimumSum(num int) int {
    digits := []int{}
    
    for num > 0 {
        digits = append(digits, num % 10)
        num /= 10
    }
    
    sort.Ints(digits)
    
    new1 := digits[0] * 10 + digits[2]
    new2 := digits[1] * 10 + digits[3]
    
    return new1 + new2
}