Greedy Florist

  • + 0 comments

    Python 3 - sort desc, caclulate multiplier by flooring the division of flower index and number of friends (this is the "round" of purchase), multiply times price, loop exists at last flower.

    def getMinimumCost(k, c):
        c = sorted(c, reverse=True) 
    
        total_cost = 0
    
        for i in range(len(c)):
            multiplier = (i // k) + 1  
            total_cost += multiplier * c[i]
    
        return total_cost