1167. Minimum Cost to Connect Sticks
https://leetcode.com/problems/minimum-cost-to-connect-sticks/
Python
import heapq
class Solution:
def connectSticks(self, sticks: List[int]) -> int:
heapq.heapify(sticks)
total_cost = 0
while len(sticks) > 1:
cost = heapq.heappop(sticks) + heapq.heappop(sticks)
total_cost += cost
heapq.heappush(sticks, cost)
return total_cost