Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1167.Minimum Cost to Connect St…
Browse files Browse the repository at this point in the history
…icks
  • Loading branch information
yanglbme committed Sep 22, 2021
1 parent 5096f6e commit 23f1710
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 4 deletions.
85 changes: 83 additions & 2 deletions solution/1100-1199/1167.Minimum Cost to Connect Sticks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,108 @@
<li><code><span>1 <= sticks[i] <= 10<sup>4</sup></span></code></li>
</ul>


## 解法

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

优先队列。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def connectSticks(self, sticks: List[int]) -> int:
h = []
for s in sticks:
heapq.heappush(h, s)
res = 0
while len(h) > 1:
val = heapq.heappop(h) + heapq.heappop(h)
res += val
heapq.heappush(h, val)
return res
```

### **Java**

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

```java
class Solution {
public int connectSticks(int[] sticks) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int s : sticks) {
pq.offer(s);
}
int res = 0;
while (pq.size() > 1) {
int val = pq.poll() + pq.poll();
res += val;
pq.offer(val);
}
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
int connectSticks(vector<int>& sticks) {
priority_queue <int, vector <int>, greater <int> > pq;
for (int x: sticks) pq.push(x);
int res = 0;
while (pq.size() > 1)
{
int val = pq.top();
pq.pop();
val += pq.top();
pq.pop();
res += val;
pq.push(val);
}
return res;
}
};
```
### **Go**
```go
func connectSticks(sticks []int) int {
h := IntHeap(sticks)
heap.Init(&h)
res := 0
for h.Len() > 1 {
val := heap.Pop(&h).(int)
val += heap.Pop(&h).(int)
res += val
heap.Push(&h, val)
}
return res
}
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,102 @@ There is only one stick left, so you are done. The total cost is 4 + 9 + 17 = 30
<li><code><span>1 &lt;= sticks[i] &lt;= 10<sup>4</sup></span></code></li>
</ul>


## Solutions

Priority queue.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def connectSticks(self, sticks: List[int]) -> int:
h = []
for s in sticks:
heapq.heappush(h, s)
res = 0
while len(h) > 1:
val = heapq.heappop(h) + heapq.heappop(h)
res += val
heapq.heappush(h, val)
return res
```

### **Java**

```java
class Solution {
public int connectSticks(int[] sticks) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int s : sticks) {
pq.offer(s);
}
int res = 0;
while (pq.size() > 1) {
int val = pq.poll() + pq.poll();
res += val;
pq.offer(val);
}
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
int connectSticks(vector<int>& sticks) {
priority_queue <int, vector <int>, greater <int> > pq;
for (int x: sticks) pq.push(x);
int res = 0;
while (pq.size() > 1)
{
int val = pq.top();
pq.pop();
val += pq.top();
pq.pop();
res += val;
pq.push(val);
}
return res;
}
};
```
### **Go**
```go
func connectSticks(sticks []int) int {
h := IntHeap(sticks)
heap.Init(&h)
res := 0
for h.Len() > 1 {
val := heap.Pop(&h).(int)
val += heap.Pop(&h).(int)
res += val
heap.Push(&h, val)
}
return res
}
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int connectSticks(vector<int>& sticks) {
priority_queue <int, vector <int>, greater <int> > pq;
for (int x: sticks) pq.push(x);
int res = 0;
while (pq.size() > 1)
{
int val = pq.top();
pq.pop();
val += pq.top();
pq.pop();
res += val;
pq.push(val);
}
return res;
}
};
28 changes: 28 additions & 0 deletions solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
func connectSticks(sticks []int) int {
h := IntHeap(sticks)
heap.Init(&h)
res := 0
for h.Len() > 1 {
val := heap.Pop(&h).(int)
val += heap.Pop(&h).(int)
res += val
heap.Push(&h, val)
}
return res
}

type IntHeap []int

func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int connectSticks(int[] sticks) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int s : sticks) {
pq.offer(s);
}
int res = 0;
while (pq.size() > 1) {
int val = pq.poll() + pq.poll();
res += val;
pq.offer(val);
}
return res;
}
}
11 changes: 11 additions & 0 deletions solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def connectSticks(self, sticks: List[int]) -> int:
h = []
for s in sticks:
heapq.heappush(h, s)
res = 0
while len(h) > 1:
val = heapq.heappop(h) + heapq.heappop(h)
res += val
heapq.heappush(h, val)
return res

0 comments on commit 23f1710

Please sign in to comment.