Skip to content

Commit 1dda487

Browse files
committed
feat: add solutions to lc problem: No.1962
No.1962.Remove Stones to Minimize the Total
1 parent 3b2e551 commit 1dda487

File tree

6 files changed

+239
-2
lines changed

6 files changed

+239
-2
lines changed

solution/1900-1999/1962.Remove Stones to Minimize the Total/README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,105 @@
5757

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

60+
**方法一:优先队列(大根堆)**
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
6365

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

6668
```python
67-
69+
class Solution:
70+
def minStoneSum(self, piles: List[int], k: int) -> int:
71+
h = []
72+
for p in piles:
73+
heappush(h, -p)
74+
for _ in range(k):
75+
p = -heappop(h)
76+
heappush(h, -((p + 1) >> 1))
77+
return -sum(h)
6878
```
6979

7080
### **Java**
7181

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

7484
```java
85+
class Solution {
86+
public int minStoneSum(int[] piles, int k) {
87+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> (b - a));
88+
for (int p : piles) {
89+
q.offer(p);
90+
}
91+
while (k-- > 0) {
92+
int p = q.poll();
93+
q.offer((p + 1) >> 1);
94+
}
95+
int ans = 0;
96+
while (!q.isEmpty()) {
97+
ans += q.poll();
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int minStoneSum(vector<int>& piles, int k) {
110+
priority_queue<int> q;
111+
for (int& p : piles) q.push(p);
112+
while (k--)
113+
{
114+
int p = q.top();
115+
q.pop();
116+
q.push((p + 1) >> 1);
117+
}
118+
int ans = 0;
119+
while (!q.empty())
120+
{
121+
ans += q.top();
122+
q.pop();
123+
}
124+
return ans;
125+
}
126+
};
127+
```
75128
129+
### **Go**
130+
131+
```go
132+
func minStoneSum(piles []int, k int) int {
133+
q := &hp{piles}
134+
heap.Init(q)
135+
for k > 0 {
136+
p := q.pop()
137+
q.push((p + 1) >> 1)
138+
k--
139+
}
140+
ans := 0
141+
for q.Len() > 0 {
142+
ans += q.pop()
143+
}
144+
return ans
145+
}
146+
147+
type hp struct{ sort.IntSlice }
148+
149+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
150+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
151+
func (h *hp) Pop() interface{} {
152+
a := h.IntSlice
153+
v := a[len(a)-1]
154+
h.IntSlice = a[:len(a)-1]
155+
return v
156+
}
157+
func (h *hp) push(v int) { heap.Push(h, v) }
158+
func (h *hp) pop() int { return heap.Pop(h).(int) }
76159
```
77160

78161
### **...**

solution/1900-1999/1962.Remove Stones to Minimize the Total/README_EN.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,94 @@ The total number of stones in [2,3,3,4] is 12.
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def minStoneSum(self, piles: List[int], k: int) -> int:
61+
h = []
62+
for p in piles:
63+
heappush(h, -p)
64+
for _ in range(k):
65+
p = -heappop(h)
66+
heappush(h, -((p + 1) >> 1))
67+
return -sum(h)
6068
```
6169

6270
### **Java**
6371

6472
```java
73+
class Solution {
74+
public int minStoneSum(int[] piles, int k) {
75+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> (b - a));
76+
for (int p : piles) {
77+
q.offer(p);
78+
}
79+
while (k-- > 0) {
80+
int p = q.poll();
81+
q.offer((p + 1) >> 1);
82+
}
83+
int ans = 0;
84+
while (!q.isEmpty()) {
85+
ans += q.poll();
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int minStoneSum(vector<int>& piles, int k) {
98+
priority_queue<int> q;
99+
for (int& p : piles) q.push(p);
100+
while (k--)
101+
{
102+
int p = q.top();
103+
q.pop();
104+
q.push((p + 1) >> 1);
105+
}
106+
int ans = 0;
107+
while (!q.empty())
108+
{
109+
ans += q.top();
110+
q.pop();
111+
}
112+
return ans;
113+
}
114+
};
115+
```
65116
117+
### **Go**
118+
119+
```go
120+
func minStoneSum(piles []int, k int) int {
121+
q := &hp{piles}
122+
heap.Init(q)
123+
for k > 0 {
124+
p := q.pop()
125+
q.push((p + 1) >> 1)
126+
k--
127+
}
128+
ans := 0
129+
for q.Len() > 0 {
130+
ans += q.pop()
131+
}
132+
return ans
133+
}
134+
135+
type hp struct{ sort.IntSlice }
136+
137+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
138+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
139+
func (h *hp) Pop() interface{} {
140+
a := h.IntSlice
141+
v := a[len(a)-1]
142+
h.IntSlice = a[:len(a)-1]
143+
return v
144+
}
145+
func (h *hp) push(v int) { heap.Push(h, v) }
146+
func (h *hp) pop() int { return heap.Pop(h).(int) }
66147
```
67148

68149
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minStoneSum(vector<int>& piles, int k) {
4+
priority_queue<int> q;
5+
for (int& p : piles) q.push(p);
6+
while (k--)
7+
{
8+
int p = q.top();
9+
q.pop();
10+
q.push((p + 1) >> 1);
11+
}
12+
int ans = 0;
13+
while (!q.empty())
14+
{
15+
ans += q.top();
16+
q.pop();
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func minStoneSum(piles []int, k int) int {
2+
q := &hp{piles}
3+
heap.Init(q)
4+
for k > 0 {
5+
p := q.pop()
6+
q.push((p + 1) >> 1)
7+
k--
8+
}
9+
ans := 0
10+
for q.Len() > 0 {
11+
ans += q.pop()
12+
}
13+
return ans
14+
}
15+
16+
type hp struct{ sort.IntSlice }
17+
18+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
19+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
20+
func (h *hp) Pop() interface{} {
21+
a := h.IntSlice
22+
v := a[len(a)-1]
23+
h.IntSlice = a[:len(a)-1]
24+
return v
25+
}
26+
func (h *hp) push(v int) { heap.Push(h, v) }
27+
func (h *hp) pop() int { return heap.Pop(h).(int) }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int minStoneSum(int[] piles, int k) {
3+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> (b - a));
4+
for (int p : piles) {
5+
q.offer(p);
6+
}
7+
while (k-- > 0) {
8+
int p = q.poll();
9+
q.offer((p + 1) >> 1);
10+
}
11+
int ans = 0;
12+
while (!q.isEmpty()) {
13+
ans += q.poll();
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minStoneSum(self, piles: List[int], k: int) -> int:
3+
h = []
4+
for p in piles:
5+
heappush(h, -p)
6+
for _ in range(k):
7+
p = -heappop(h)
8+
heappush(h, -((p + 1) >> 1))
9+
return -sum(h)

0 commit comments

Comments
 (0)