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 ff04023

Browse files
feat: add solutions to lc problem: No.2208 (doocs#3537)
No.2208.Minimum Operations to Halve Array Sum
1 parent ec27a55 commit ff04023

File tree

8 files changed

+98
-194
lines changed

8 files changed

+98
-194
lines changed

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

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ nums 的和减小了 31 - 14.5 = 16.5 ,减小的部分超过了初始数组和
8989
class Solution:
9090
def halveArray(self, nums: List[int]) -> int:
9191
s = sum(nums) / 2
92-
h = []
93-
for v in nums:
94-
heappush(h, -v)
92+
pq = []
93+
for x in nums:
94+
heappush(pq, -x)
9595
ans = 0
9696
while s > 0:
97-
t = -heappop(h) / 2
97+
t = -heappop(pq) / 2
9898
s -= t
99-
heappush(h, -t)
99+
heappush(pq, -t)
100100
ans += 1
101101
return ans
102102
```
@@ -106,18 +106,18 @@ class Solution:
106106
```java
107107
class Solution {
108108
public int halveArray(int[] nums) {
109+
PriorityQueue<Double> pq = new PriorityQueue<>(Collections.reverseOrder());
109110
double s = 0;
110-
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
111-
for (int v : nums) {
112-
q.offer(v * 1.0);
113-
s += v;
111+
for (int x : nums) {
112+
s += x;
113+
pq.offer((double) x);
114114
}
115115
s /= 2.0;
116116
int ans = 0;
117117
while (s > 0) {
118-
double t = q.poll();
119-
s -= t/2.0;
120-
q.offer(t/2.0);
118+
double t = pq.poll()/2.0;
119+
s -= t;
120+
pq.offer(t);
121121
++ans;
122122
}
123123
return ans;
@@ -131,19 +131,19 @@ class Solution {
131131
class Solution {
132132
public:
133133
int halveArray(vector<int>& nums) {
134-
priority_queue<double> q;
134+
priority_queue<double> pq;
135135
double s = 0;
136-
for (int& v : nums) {
137-
s += v;
138-
q.push(v);
136+
for (int x : nums) {
137+
s += x;
138+
pq.push((double) x);
139139
}
140140
s /= 2.0;
141141
int ans = 0;
142142
while (s > 0) {
143-
double t = q.top() / 2;
144-
q.pop();
143+
double t = pq.top() / 2.0;
144+
pq.pop();
145145
s -= t;
146-
q.push(t);
146+
pq.push(t);
147147
++ans;
148148
}
149149
return ans;
@@ -156,17 +156,17 @@ public:
156156
```go
157157
func halveArray(nums []int) (ans int) {
158158
var s float64
159-
q := hp{}
159+
pq := &hp{}
160160
for _, x := range nums {
161161
s += float64(x)
162-
heap.Push(&q, float64(x))
162+
heap.Push(pq, float64(x))
163163
}
164164
s /= 2
165165
for s > 0 {
166-
x := heap.Pop(&q).(float64)
166+
t := heap.Pop(pq).(float64) / 2
167+
s -= t
167168
ans++
168-
s -= x / 2
169-
heap.Push(&q, x/2)
169+
heap.Push(pq, t)
170170
}
171171
return
172172
}
@@ -188,17 +188,16 @@ func (h *hp) Pop() any {
188188
```ts
189189
function halveArray(nums: number[]): number {
190190
let s: number = nums.reduce((a, b) => a + b) / 2;
191-
const h = new MaxPriorityQueue();
192-
for (const v of nums) {
193-
h.enqueue(v, v);
191+
const pq = new MaxPriorityQueue();
192+
for (const x of nums) {
193+
pq.enqueue(x, x);
194194
}
195-
let ans:number = 0;
195+
let ans = 0;
196196
while (s > 0) {
197-
let { element: t } = h.dequeue();
198-
t /= 2;
197+
const t = pq.dequeue().element / 2;
199198
s -= t;
200-
h.enqueue(t, t);
201-
ans+=1;
199+
++ans;
200+
pq.enqueue(t, t);
202201
}
203202
return ans;
204203
}
@@ -208,40 +207,4 @@ function halveArray(nums: number[]): number {
208207

209208
<!-- solution:end -->
210209

211-
<!-- solution:start -->
212-
213-
### 方法二
214-
215-
<!-- tabs:start -->
216-
217-
#### Go
218-
219-
```go
220-
func halveArray(nums []int) (ans int) {
221-
half := 0
222-
for i := range nums {
223-
nums[i] <<= 20
224-
half += nums[i]
225-
}
226-
h := hp{nums}
227-
heap.Init(&h)
228-
for half >>= 1; half > 0; ans++ {
229-
half -= h.IntSlice[0] >> 1
230-
h.IntSlice[0] >>= 1
231-
heap.Fix(&h, 0)
232-
}
233-
return
234-
}
235-
236-
type hp struct{ sort.IntSlice }
237-
238-
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
239-
func (hp) Push(any) {}
240-
func (hp) Pop() (_ any) { return }
241-
```
242-
243-
<!-- tabs:end -->
244-
245-
<!-- solution:end -->
246-
247210
<!-- problem:end -->

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

Lines changed: 33 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The following is one of the ways to reduce the sum by at least half:
3535
Pick the number 19 and reduce it to 9.5.
3636
Pick the number 9.5 and reduce it to 4.75.
3737
Pick the number 8 and reduce it to 4.
38-
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75.
38+
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75.
3939
The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 &gt;= 33/2 = 16.5.
4040
Overall, 3 operations were used so we return 3.
4141
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
@@ -51,7 +51,7 @@ The following is one of the ways to reduce the sum by at least half:
5151
Pick the number 20 and reduce it to 10.
5252
Pick the number 10 and reduce it to 5.
5353
Pick the number 3 and reduce it to 1.5.
54-
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5.
54+
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5.
5555
The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 &gt;= 31/2 = 15.5.
5656
Overall, 3 operations were used so we return 3.
5757
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
@@ -87,14 +87,14 @@ The time complexity is $O(n \times \log n),ドル and the space complexity is $O(n)$.
8787
class Solution:
8888
def halveArray(self, nums: List[int]) -> int:
8989
s = sum(nums) / 2
90-
h = []
91-
for v in nums:
92-
heappush(h, -v)
90+
pq = []
91+
for x in nums:
92+
heappush(pq, -x)
9393
ans = 0
9494
while s > 0:
95-
t = -heappop(h) / 2
95+
t = -heappop(pq) / 2
9696
s -= t
97-
heappush(h, -t)
97+
heappush(pq, -t)
9898
ans += 1
9999
return ans
100100
```
@@ -104,18 +104,18 @@ class Solution:
104104
```java
105105
class Solution {
106106
public int halveArray(int[] nums) {
107+
PriorityQueue<Double> pq = new PriorityQueue<>(Collections.reverseOrder());
107108
double s = 0;
108-
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
109-
for (int v : nums) {
110-
q.offer(v * 1.0);
111-
s += v;
109+
for (int x : nums) {
110+
s += x;
111+
pq.offer((double) x);
112112
}
113113
s /= 2.0;
114114
int ans = 0;
115115
while (s > 0) {
116-
double t = q.poll();
117-
s -= t/2.0;
118-
q.offer(t/2.0);
116+
double t = pq.poll()/2.0;
117+
s -= t;
118+
pq.offer(t);
119119
++ans;
120120
}
121121
return ans;
@@ -129,19 +129,19 @@ class Solution {
129129
class Solution {
130130
public:
131131
int halveArray(vector<int>& nums) {
132-
priority_queue<double> q;
132+
priority_queue<double> pq;
133133
double s = 0;
134-
for (int& v : nums) {
135-
s += v;
136-
q.push(v);
134+
for (int x : nums) {
135+
s += x;
136+
pq.push((double) x);
137137
}
138138
s /= 2.0;
139139
int ans = 0;
140140
while (s > 0) {
141-
double t = q.top() / 2;
142-
q.pop();
141+
double t = pq.top() / 2.0;
142+
pq.pop();
143143
s -= t;
144-
q.push(t);
144+
pq.push(t);
145145
++ans;
146146
}
147147
return ans;
@@ -154,17 +154,17 @@ public:
154154
```go
155155
func halveArray(nums []int) (ans int) {
156156
var s float64
157-
q := hp{}
157+
pq := &hp{}
158158
for _, x := range nums {
159159
s += float64(x)
160-
heap.Push(&q, float64(x))
160+
heap.Push(pq, float64(x))
161161
}
162162
s /= 2
163163
for s > 0 {
164-
x := heap.Pop(&q).(float64)
164+
t := heap.Pop(pq).(float64) / 2
165+
s -= t
165166
ans++
166-
s -= x / 2
167-
heap.Push(&q, x/2)
167+
heap.Push(pq, t)
168168
}
169169
return
170170
}
@@ -186,17 +186,16 @@ func (h *hp) Pop() any {
186186
```ts
187187
function halveArray(nums: number[]): number {
188188
let s: number = nums.reduce((a, b) => a + b) / 2;
189-
const h = new MaxPriorityQueue();
190-
for (const v of nums) {
191-
h.enqueue(v, v);
189+
const pq = new MaxPriorityQueue();
190+
for (const x of nums) {
191+
pq.enqueue(x, x);
192192
}
193-
let ans:number = 0;
193+
let ans = 0;
194194
while (s > 0) {
195-
let { element: t } = h.dequeue();
196-
t /= 2;
195+
const t = pq.dequeue().element / 2;
197196
s -= t;
198-
h.enqueue(t, t);
199-
ans+=1;
197+
++ans;
198+
pq.enqueue(t, t);
200199
}
201200
return ans;
202201
}
@@ -206,40 +205,4 @@ function halveArray(nums: number[]): number {
206205

207206
<!-- solution:end -->
208207

209-
<!-- solution:start -->
210-
211-
### Solution 2
212-
213-
<!-- tabs:start -->
214-
215-
#### Go
216-
217-
```go
218-
func halveArray(nums []int) (ans int) {
219-
half := 0
220-
for i := range nums {
221-
nums[i] <<= 20
222-
half += nums[i]
223-
}
224-
h := hp{nums}
225-
heap.Init(&h)
226-
for half >>= 1; half > 0; ans++ {
227-
half -= h.IntSlice[0] >> 1
228-
h.IntSlice[0] >>= 1
229-
heap.Fix(&h, 0)
230-
}
231-
return
232-
}
233-
234-
type hp struct{ sort.IntSlice }
235-
236-
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
237-
func (hp) Push(any) {}
238-
func (hp) Pop() (_ any) { return }
239-
```
240-
241-
<!-- tabs:end -->
242-
243-
<!-- solution:end -->
244-
245208
<!-- problem:end -->
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
class Solution {
22
public:
33
int halveArray(vector<int>& nums) {
4-
priority_queue<double> q;
4+
priority_queue<double> pq;
55
double s = 0;
6-
for (int& v : nums) {
7-
s += v;
8-
q.push(v);
6+
for (int x : nums) {
7+
s += x;
8+
pq.push((double) x);
99
}
1010
s /= 2.0;
1111
int ans = 0;
1212
while (s > 0) {
13-
double t = q.top() / 2;
14-
q.pop();
13+
double t = pq.top() / 2.0;
14+
pq.pop();
1515
s -= t;
16-
q.push(t);
16+
pq.push(t);
1717
++ans;
1818
}
1919
return ans;
2020
}
21-
};
21+
};

0 commit comments

Comments
(0)

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