Greedy Florist

  • + 0 comments

    c# solution:

    static int getMinimumCost(int k, int[] c) {
    
         int howManyFlowers = c.Count();
         Array.Sort(c);
        int cost = 0;
        int multipleCost = 1;
        int couterForMultipleCost = 0;
        if (howManyFlowers <= k)
        {
            //cost = c.Sum();
            return c.Sum();
        }
        else
        {
            do
            {
            couterForMultipleCost++;
            if (couterForMultipleCost > k)
            {
                couterForMultipleCost = 1;
                multipleCost++;
            }
            cost += multipleCost * c[howManyFlowers - 1];
            howManyFlowers -= 1;
            } while (howManyFlowers != 0);
        }
        return cost;
    }