Skip to content

Commit 8314568

Browse files
authored
Create minimum-cost-to-connect-sticks.py
1 parent 8058d69 commit 8314568

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
import heapq
5+
6+
7+
class Solution(object):
8+
def connectSticks(self, sticks):
9+
"""
10+
:type sticks: List[int]
11+
:rtype: int
12+
"""
13+
heapq.heapify(sticks)
14+
result = 0
15+
while len(sticks) > 1:
16+
x, y = heapq.heappop(sticks), heapq.heappop(sticks)
17+
result += x+y
18+
heapq.heappush(sticks, x+y)
19+
return result

0 commit comments

Comments
 (0)