Skip to content

Commit e86cfbc

Browse files
committed
feat: add solutions to lc problem: No.2208
No.2208.Minimum Operations to Halve Array Sum
1 parent 7be8b9e commit e86cfbc

File tree

5 files changed

+194
-12
lines changed

5 files changed

+194
-12
lines changed

solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README.md

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,73 @@ nums 的和减小了 31 - 14.5 = 16.5 ,减小的部分超过了初始数组和
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def halveArray(self, nums: List[int]) -> int:
67+
s = sum(nums) / 2
68+
h = []
69+
for v in nums:
70+
heappush(h, -v)
71+
ans = 0
72+
while s > 0:
73+
t = -heappop(h) / 2
74+
s -= t
75+
heappush(h, -t)
76+
ans += 1
77+
return ans
6678
```
6779

6880
### **Java**
6981

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

7284
```java
73-
85+
class Solution {
86+
public int halveArray(int[] nums) {
87+
long s = 0;
88+
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
89+
for (int v : nums) {
90+
q.offer(v * 1.0);
91+
s += v;
92+
}
93+
double d = s / 2.0;
94+
int ans = 0;
95+
while (d > 0) {
96+
double t = q.poll();
97+
d -= t / 2.0;
98+
q.offer(t / 2.0);
99+
++ans;
100+
}
101+
return ans;
102+
}
103+
}
74104
```
75105

76-
### **TypeScript**
77-
78-
```ts
79-
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int halveArray(vector<int>& nums) {
112+
priority_queue<double> q;
113+
long long s = 0;
114+
for (int& v : nums)
115+
{
116+
s += v;
117+
q.push(v);
118+
}
119+
double d = s / 2.0;
120+
int ans = 0;
121+
while (d > 0)
122+
{
123+
double t = q.top() / 2;
124+
q.pop();
125+
d -= t;
126+
q.push(t);
127+
++ans;
128+
}
129+
return ans;
130+
}
131+
};
80132
```
81133
82134
### **Go**
@@ -103,6 +155,18 @@ type hp struct{ sort.IntSlice }
103155
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
104156
func (hp) Push(interface{}) {}
105157
func (hp) Pop() (_ interface{}) { return }
158+
```
159+
160+
161+
### **TypeScript**
162+
163+
```ts
164+
165+
```
166+
167+
### **...**
168+
169+
```
106170
107171
```
108172

solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,71 @@ It can be shown that we cannot reduce the sum by at least half in less than 3 op
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def halveArray(self, nums: List[int]) -> int:
61+
s = sum(nums) / 2
62+
h = []
63+
for v in nums:
64+
heappush(h, -v)
65+
ans = 0
66+
while s > 0:
67+
t = -heappop(h) / 2
68+
s -= t
69+
heappush(h, -t)
70+
ans += 1
71+
return ans
6072
```
6173

6274
### **Java**
6375

6476
```java
65-
77+
class Solution {
78+
public int halveArray(int[] nums) {
79+
long s = 0;
80+
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
81+
for (int v : nums) {
82+
q.offer(v * 1.0);
83+
s += v;
84+
}
85+
double d = s / 2.0;
86+
int ans = 0;
87+
while (d > 0) {
88+
double t = q.poll();
89+
d -= t / 2.0;
90+
q.offer(t / 2.0);
91+
++ans;
92+
}
93+
return ans;
94+
}
95+
}
6696
```
6797

68-
### **TypeScript**
69-
70-
```ts
71-
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int halveArray(vector<int>& nums) {
104+
priority_queue<double> q;
105+
long long s = 0;
106+
for (int& v : nums)
107+
{
108+
s += v;
109+
q.push(v);
110+
}
111+
double d = s / 2.0;
112+
int ans = 0;
113+
while (d > 0)
114+
{
115+
double t = q.top() / 2;
116+
q.pop();
117+
d -= t;
118+
q.push(t);
119+
++ans;
120+
}
121+
return ans;
122+
}
123+
};
72124
```
73125
74126
### **Go**
@@ -95,6 +147,17 @@ type hp struct{ sort.IntSlice }
95147
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
96148
func (hp) Push(interface{}) {}
97149
func (hp) Pop() (_ interface{}) { return }
150+
```
151+
152+
### **TypeScript**
153+
154+
```ts
155+
156+
```
157+
158+
### **...**
159+
160+
```
98161
99162
```
100163

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int halveArray(vector<int>& nums) {
4+
priority_queue<double> q;
5+
long long s = 0;
6+
for (int& v : nums)
7+
{
8+
s += v;
9+
q.push(v);
10+
}
11+
double d = s / 2.0;
12+
int ans = 0;
13+
while (d > 0)
14+
{
15+
double t = q.top() / 2;
16+
q.pop();
17+
d -= t;
18+
q.push(t);
19+
++ans;
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int halveArray(int[] nums) {
3+
long s = 0;
4+
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
5+
for (int v : nums) {
6+
q.offer(v * 1.0);
7+
s += v;
8+
}
9+
double d = s / 2.0;
10+
int ans = 0;
11+
while (d > 0) {
12+
double t = q.poll();
13+
d -= t / 2.0;
14+
q.offer(t / 2.0);
15+
++ans;
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def halveArray(self, nums: List[int]) -> int:
3+
s = sum(nums) / 2
4+
h = []
5+
for v in nums:
6+
heappush(h, -v)
7+
ans = 0
8+
while s > 0:
9+
t = -heappop(h) / 2
10+
s -= t
11+
heappush(h, -t)
12+
ans += 1
13+
return ans

0 commit comments

Comments
 (0)