Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 523 Bytes

1872.-minimum-cost-to-connect-sticks.md

File metadata and controls

24 lines (19 loc) · 523 Bytes

1872. Minimum Cost to Connect Sticks

class Solution:
    """
    @param sticks: the length of sticks
    @return: Minimum Cost to Connect Sticks
    """
    def MinimumCost(self, sticks):
        # write your code here
        import heapq
        heapq.heapify(sticks)
        
        ans = 0
        while len(sticks) > 1:
            x = heapq.heappop(sticks)
            y = heapq.heappop(sticks)
            ans += x+y
            heapq.heappush(sticks,x+y)
            
        return ans