Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23f1710

Browse files
committed
feat: add solutions to lc problem: No.1167.Minimum Cost to Connect Sticks
1 parent 5096f6e commit 23f1710

File tree

6 files changed

+238
-4
lines changed

6 files changed

+238
-4
lines changed

‎solution/1100-1199/1167.Minimum Cost to Connect Sticks/README.md‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,108 @@
5454
<li><code><span>1 <= sticks[i] <= 10<sup>4</sup></span></code></li>
5555
</ul>
5656

57-
5857
## 解法
5958

6059
<!-- 这里可写通用的实现逻辑 -->
6160

61+
优先队列。
62+
6263
<!-- tabs:start -->
6364

6465
### **Python3**
6566

6667
<!-- 这里可写当前语言的特殊实现逻辑 -->
6768

6869
```python
69-
70+
class Solution:
71+
def connectSticks(self, sticks: List[int]) -> int:
72+
h = []
73+
for s in sticks:
74+
heapq.heappush(h, s)
75+
res = 0
76+
while len(h) > 1:
77+
val = heapq.heappop(h) + heapq.heappop(h)
78+
res += val
79+
heapq.heappush(h, val)
80+
return res
7081
```
7182

7283
### **Java**
7384

7485
<!-- 这里可写当前语言的特殊实现逻辑 -->
7586

7687
```java
88+
class Solution {
89+
public int connectSticks(int[] sticks) {
90+
PriorityQueue<Integer> pq = new PriorityQueue<>();
91+
for (int s : sticks) {
92+
pq.offer(s);
93+
}
94+
int res = 0;
95+
while (pq.size() > 1) {
96+
int val = pq.poll() + pq.poll();
97+
res += val;
98+
pq.offer(val);
99+
}
100+
return res;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int connectSticks(vector<int>& sticks) {
111+
priority_queue <int, vector <int>, greater <int> > pq;
112+
for (int x: sticks) pq.push(x);
113+
int res = 0;
114+
while (pq.size() > 1)
115+
{
116+
int val = pq.top();
117+
pq.pop();
118+
val += pq.top();
119+
pq.pop();
120+
res += val;
121+
pq.push(val);
122+
}
123+
return res;
124+
}
125+
};
126+
```
77127
128+
### **Go**
129+
130+
```go
131+
func connectSticks(sticks []int) int {
132+
h := IntHeap(sticks)
133+
heap.Init(&h)
134+
res := 0
135+
for h.Len() > 1 {
136+
val := heap.Pop(&h).(int)
137+
val += heap.Pop(&h).(int)
138+
res += val
139+
heap.Push(&h, val)
140+
}
141+
return res
142+
}
143+
144+
type IntHeap []int
145+
146+
func (h IntHeap) Len() int { return len(h) }
147+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
148+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
149+
func (h *IntHeap) Push(x interface{}) {
150+
*h = append(*h, x.(int))
151+
}
152+
func (h *IntHeap) Pop() interface{} {
153+
old := *h
154+
n := len(old)
155+
x := old[n-1]
156+
*h = old[0 : n-1]
157+
return x
158+
}
78159
```
79160

80161
### **...**

‎solution/1100-1199/1167.Minimum Cost to Connect Sticks/README_EN.md‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,102 @@ There is only one stick left, so you are done. The total cost is 4 + 9 + 17 = 30
5050
<li><code><span>1 &lt;= sticks[i] &lt;= 10<sup>4</sup></span></code></li>
5151
</ul>
5252

53-
5453
## Solutions
5554

55+
Priority queue.
56+
5657
<!-- tabs:start -->
5758

5859
### **Python3**
5960

6061
```python
61-
62+
class Solution:
63+
def connectSticks(self, sticks: List[int]) -> int:
64+
h = []
65+
for s in sticks:
66+
heapq.heappush(h, s)
67+
res = 0
68+
while len(h) > 1:
69+
val = heapq.heappop(h) + heapq.heappop(h)
70+
res += val
71+
heapq.heappush(h, val)
72+
return res
6273
```
6374

6475
### **Java**
6576

6677
```java
78+
class Solution {
79+
public int connectSticks(int[] sticks) {
80+
PriorityQueue<Integer> pq = new PriorityQueue<>();
81+
for (int s : sticks) {
82+
pq.offer(s);
83+
}
84+
int res = 0;
85+
while (pq.size() > 1) {
86+
int val = pq.poll() + pq.poll();
87+
res += val;
88+
pq.offer(val);
89+
}
90+
return res;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int connectSticks(vector<int>& sticks) {
101+
priority_queue <int, vector <int>, greater <int> > pq;
102+
for (int x: sticks) pq.push(x);
103+
int res = 0;
104+
while (pq.size() > 1)
105+
{
106+
int val = pq.top();
107+
pq.pop();
108+
val += pq.top();
109+
pq.pop();
110+
res += val;
111+
pq.push(val);
112+
}
113+
return res;
114+
}
115+
};
116+
```
67117
118+
### **Go**
119+
120+
```go
121+
func connectSticks(sticks []int) int {
122+
h := IntHeap(sticks)
123+
heap.Init(&h)
124+
res := 0
125+
for h.Len() > 1 {
126+
val := heap.Pop(&h).(int)
127+
val += heap.Pop(&h).(int)
128+
res += val
129+
heap.Push(&h, val)
130+
}
131+
return res
132+
}
133+
134+
type IntHeap []int
135+
136+
func (h IntHeap) Len() int { return len(h) }
137+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
138+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
139+
func (h *IntHeap) Push(x interface{}) {
140+
*h = append(*h, x.(int))
141+
}
142+
func (h *IntHeap) Pop() interface{} {
143+
old := *h
144+
n := len(old)
145+
x := old[n-1]
146+
*h = old[0 : n-1]
147+
return x
148+
}
68149
```
69150

70151
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int connectSticks(vector<int>& sticks) {
4+
priority_queue <int, vector <int>, greater <int> > pq;
5+
for (int x: sticks) pq.push(x);
6+
int res = 0;
7+
while (pq.size() > 1)
8+
{
9+
int val = pq.top();
10+
pq.pop();
11+
val += pq.top();
12+
pq.pop();
13+
res += val;
14+
pq.push(val);
15+
}
16+
return res;
17+
}
18+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func connectSticks(sticks []int) int {
2+
h := IntHeap(sticks)
3+
heap.Init(&h)
4+
res := 0
5+
for h.Len() > 1 {
6+
val := heap.Pop(&h).(int)
7+
val += heap.Pop(&h).(int)
8+
res += val
9+
heap.Push(&h, val)
10+
}
11+
return res
12+
}
13+
14+
type IntHeap []int
15+
16+
func (h IntHeap) Len() int { return len(h) }
17+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
18+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
19+
func (h *IntHeap) Push(x interface{}) {
20+
*h = append(*h, x.(int))
21+
}
22+
func (h *IntHeap) Pop() interface{} {
23+
old := *h
24+
n := len(old)
25+
x := old[n-1]
26+
*h = old[0 : n-1]
27+
return x
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int connectSticks(int[] sticks) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>();
4+
for (int s : sticks) {
5+
pq.offer(s);
6+
}
7+
int res = 0;
8+
while (pq.size() > 1) {
9+
int val = pq.poll() + pq.poll();
10+
res += val;
11+
pq.offer(val);
12+
}
13+
return res;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def connectSticks(self, sticks: List[int]) -> int:
3+
h = []
4+
for s in sticks:
5+
heapq.heappush(h, s)
6+
res = 0
7+
while len(h) > 1:
8+
val = heapq.heappop(h) + heapq.heappop(h)
9+
res += val
10+
heapq.heappush(h, val)
11+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /