Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2208 (#3537)
Browse files Browse the repository at this point in the history
No.2208.Minimum Operations to Halve Array Sum
  • Loading branch information
yanglbme authored Sep 19, 2024
1 parent ec27a55 commit ff04023
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ nums 的和减小了 31 - 14.5 = 16.5 ,减小的部分超过了初始数组和
class Solution:
def halveArray(self, nums: List[int]) -> int:
s = sum(nums) / 2
h = []
for v in nums:
heappush(h, -v)
pq = []
for x in nums:
heappush(pq, -x)
ans = 0
while s > 0:
t = -heappop(h) / 2
t = -heappop(pq) / 2
s -= t
heappush(h, -t)
heappush(pq, -t)
ans += 1
return ans
```
Expand All @@ -106,18 +106,18 @@ class Solution:
```java
class Solution {
public int halveArray(int[] nums) {
PriorityQueue<Double> pq = new PriorityQueue<>(Collections.reverseOrder());
double s = 0;
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
for (int v : nums) {
q.offer(v * 1.0);
s += v;
for (int x : nums) {
s += x;
pq.offer((double) x);
}
s /= 2.0;
int ans = 0;
while (s > 0) {
double t = q.poll();
s -= t / 2.0;
q.offer(t / 2.0);
double t = pq.poll() / 2.0;
s -= t;
pq.offer(t);
++ans;
}
return ans;
Expand All @@ -131,19 +131,19 @@ class Solution {
class Solution {
public:
int halveArray(vector<int>& nums) {
priority_queue<double> q;
priority_queue<double> pq;
double s = 0;
for (int& v : nums) {
s += v;
q.push(v);
for (int x : nums) {
s += x;
pq.push((double) x);
}
s /= 2.0;
int ans = 0;
while (s > 0) {
double t = q.top() / 2;
q.pop();
double t = pq.top() / 2.0;
pq.pop();
s -= t;
q.push(t);
pq.push(t);
++ans;
}
return ans;
Expand All @@ -156,17 +156,17 @@ public:
```go
func halveArray(nums []int) (ans int) {
var s float64
q := hp{}
pq := &hp{}
for _, x := range nums {
s += float64(x)
heap.Push(&q, float64(x))
heap.Push(pq, float64(x))
}
s /= 2
for s > 0 {
x := heap.Pop(&q).(float64)
t := heap.Pop(pq).(float64) / 2
s -= t
ans++
s -= x / 2
heap.Push(&q, x/2)
heap.Push(pq, t)
}
return
}
Expand All @@ -188,17 +188,16 @@ func (h *hp) Pop() any {
```ts
function halveArray(nums: number[]): number {
let s: number = nums.reduce((a, b) => a + b) / 2;
const h = new MaxPriorityQueue();
for (const v of nums) {
h.enqueue(v, v);
const pq = new MaxPriorityQueue();
for (const x of nums) {
pq.enqueue(x, x);
}
let ans: number = 0;
let ans = 0;
while (s > 0) {
let { element: t } = h.dequeue();
t /= 2;
const t = pq.dequeue().element / 2;
s -= t;
h.enqueue(t, t);
ans += 1;
++ans;
pq.enqueue(t, t);
}
return ans;
}
Expand All @@ -208,40 +207,4 @@ function halveArray(nums: number[]): number {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Go

```go
func halveArray(nums []int) (ans int) {
half := 0
for i := range nums {
nums[i] <<= 20
half += nums[i]
}
h := hp{nums}
heap.Init(&h)
for half >>= 1; half > 0; ans++ {
half -= h.IntSlice[0] >> 1
h.IntSlice[0] >>= 1
heap.Fix(&h, 0)
}
return
}

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
func (hp) Push(any) {}
func (hp) Pop() (_ any) { return }
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The following is one of the ways to reduce the sum by at least half:
Pick the number 19 and reduce it to 9.5.
Pick the number 9.5 and reduce it to 4.75.
Pick the number 8 and reduce it to 4.
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75.
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75.
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.
Overall, 3 operations were used so we return 3.
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
Expand All @@ -51,7 +51,7 @@ The following is one of the ways to reduce the sum by at least half:
Pick the number 20 and reduce it to 10.
Pick the number 10 and reduce it to 5.
Pick the number 3 and reduce it to 1.5.
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5.
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5.
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.
Overall, 3 operations were used so we return 3.
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.
Expand Down Expand Up @@ -87,14 +87,14 @@ The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$.
class Solution:
def halveArray(self, nums: List[int]) -> int:
s = sum(nums) / 2
h = []
for v in nums:
heappush(h, -v)
pq = []
for x in nums:
heappush(pq, -x)
ans = 0
while s > 0:
t = -heappop(h) / 2
t = -heappop(pq) / 2
s -= t
heappush(h, -t)
heappush(pq, -t)
ans += 1
return ans
```
Expand All @@ -104,18 +104,18 @@ class Solution:
```java
class Solution {
public int halveArray(int[] nums) {
PriorityQueue<Double> pq = new PriorityQueue<>(Collections.reverseOrder());
double s = 0;
PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
for (int v : nums) {
q.offer(v * 1.0);
s += v;
for (int x : nums) {
s += x;
pq.offer((double) x);
}
s /= 2.0;
int ans = 0;
while (s > 0) {
double t = q.poll();
s -= t / 2.0;
q.offer(t / 2.0);
double t = pq.poll() / 2.0;
s -= t;
pq.offer(t);
++ans;
}
return ans;
Expand All @@ -129,19 +129,19 @@ class Solution {
class Solution {
public:
int halveArray(vector<int>& nums) {
priority_queue<double> q;
priority_queue<double> pq;
double s = 0;
for (int& v : nums) {
s += v;
q.push(v);
for (int x : nums) {
s += x;
pq.push((double) x);
}
s /= 2.0;
int ans = 0;
while (s > 0) {
double t = q.top() / 2;
q.pop();
double t = pq.top() / 2.0;
pq.pop();
s -= t;
q.push(t);
pq.push(t);
++ans;
}
return ans;
Expand All @@ -154,17 +154,17 @@ public:
```go
func halveArray(nums []int) (ans int) {
var s float64
q := hp{}
pq := &hp{}
for _, x := range nums {
s += float64(x)
heap.Push(&q, float64(x))
heap.Push(pq, float64(x))
}
s /= 2
for s > 0 {
x := heap.Pop(&q).(float64)
t := heap.Pop(pq).(float64) / 2
s -= t
ans++
s -= x / 2
heap.Push(&q, x/2)
heap.Push(pq, t)
}
return
}
Expand All @@ -186,17 +186,16 @@ func (h *hp) Pop() any {
```ts
function halveArray(nums: number[]): number {
let s: number = nums.reduce((a, b) => a + b) / 2;
const h = new MaxPriorityQueue();
for (const v of nums) {
h.enqueue(v, v);
const pq = new MaxPriorityQueue();
for (const x of nums) {
pq.enqueue(x, x);
}
let ans: number = 0;
let ans = 0;
while (s > 0) {
let { element: t } = h.dequeue();
t /= 2;
const t = pq.dequeue().element / 2;
s -= t;
h.enqueue(t, t);
ans += 1;
++ans;
pq.enqueue(t, t);
}
return ans;
}
Expand All @@ -206,40 +205,4 @@ function halveArray(nums: number[]): number {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Go

```go
func halveArray(nums []int) (ans int) {
half := 0
for i := range nums {
nums[i] <<= 20
half += nums[i]
}
h := hp{nums}
heap.Init(&h)
for half >>= 1; half > 0; ans++ {
half -= h.IntSlice[0] >> 1
h.IntSlice[0] >>= 1
heap.Fix(&h, 0)
}
return
}

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
func (hp) Push(any) {}
func (hp) Pop() (_ any) { return }
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
class Solution {
public:
int halveArray(vector<int>& nums) {
priority_queue<double> q;
priority_queue<double> pq;
double s = 0;
for (int& v : nums) {
s += v;
q.push(v);
for (int x : nums) {
s += x;
pq.push((double) x);
}
s /= 2.0;
int ans = 0;
while (s > 0) {
double t = q.top() / 2;
q.pop();
double t = pq.top() / 2.0;
pq.pop();
s -= t;
q.push(t);
pq.push(t);
++ans;
}
return ans;
}
};
};
Loading

0 comments on commit ff04023

Please sign in to comment.