题目:原题链接(中等)
标签:贪心算法、堆
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N l o g N ) O(NlogN) O(NlogN) | O ( N ) O(N) O(N) | 184ms (63.49%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution:
def connectSticks(self, sticks: List[int]) -> int:
ans = 0
heapq.heapify(sticks)
while len(sticks) >= 2:
n1 = heapq.heappop(sticks)
n2 = heapq.heappop(sticks)
ans += n1 + n2
heapq.heappush(sticks, n1 + n2)
return ans