Skip to content

Commit a1044cb

Browse files
authored
Create 1167.Minimum-Cost-to-Connect-Sticks.cpp
1 parent 0cc88b6 commit a1044cb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int connectSticks(vector<int>& sticks)
4+
{
5+
multiset<int>Set;
6+
int sum = 0;
7+
for (auto x: sticks)
8+
{
9+
Set.insert(x);
10+
}
11+
for (int i=0; i<sticks.size()-1; i++)
12+
{
13+
int x = *(Set.begin());
14+
Set.erase(Set.begin());
15+
int y = *(Set.begin());
16+
Set.erase(Set.begin());
17+
sum += x+y;
18+
Set.insert(x+y);
19+
}
20+
return sum;
21+
}
22+
};

0 commit comments

Comments
 (0)