Leetcode Note: Go - Minimum Cost of Buying Candies With Discount

Minimum Cost of Buying Candies With Discount - LeetCode
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/

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

回答

go - Minimum Cost of Buying Candies With Discount - LeetCode
https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/solutions/3576285/go/

func minimumCost(cost []int) int {
    sort.Slice(cost, func(i, j int) bool {
        return cost[i] > cost[j]
    });


    res := 0;
    counter := 0;

    for _, c := range cost {
        if counter == 2 {
            counter = 0;
            continue
        }
        res += c
        counter++;
    }
    return res
}