diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README.md b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README.md index 0318fc275ad1f..48d0e65e023cf 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README.md +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README.md @@ -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 ``` @@ -106,18 +106,18 @@ class Solution: ```java class Solution { public int halveArray(int[] nums) { + PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); double s = 0; - PriorityQueue 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; @@ -131,19 +131,19 @@ class Solution { class Solution { public: int halveArray(vector& nums) { - priority_queue q; + priority_queue 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; @@ -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 } @@ -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; } @@ -208,40 +207,4 @@ function halveArray(nums: number[]): number { - - -### 方法二 - - - -#### 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 } -``` - - - - - diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md index 80146fe2ea082..4b97b0cbba73f 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md @@ -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 >= 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. @@ -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 >= 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. @@ -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 ``` @@ -104,18 +104,18 @@ class Solution: ```java class Solution { public int halveArray(int[] nums) { + PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); double s = 0; - PriorityQueue 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; @@ -129,19 +129,19 @@ class Solution { class Solution { public: int halveArray(vector& nums) { - priority_queue q; + priority_queue 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; @@ -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 } @@ -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; } @@ -206,40 +205,4 @@ function halveArray(nums: number[]): number { - - -### Solution 2 - - - -#### 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 } -``` - - - - - diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp index ba5eefd57b0a9..e5fee2f16aa58 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp @@ -1,21 +1,21 @@ class Solution { public: int halveArray(vector& nums) { - priority_queue q; + priority_queue 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; } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go index e3d7dae5b4b2f..17e9fd76cbf47 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go @@ -1,16 +1,16 @@ 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 } @@ -24,4 +24,4 @@ func (h *hp) Pop() any { v := a[len(a)-1] h.Float64Slice = a[:len(a)-1] return v -} \ No newline at end of file +} diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.java b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.java index 8a12cf6776e6e..83d1954658753 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.java +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.java @@ -1,19 +1,19 @@ class Solution { public int halveArray(int[] nums) { + PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); double s = 0; - PriorityQueue 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; } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.py b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.py index c1751442144cb..c55be1382e97d 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.py +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.py @@ -1,13 +1,13 @@ 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 diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.ts b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.ts index 1a5e33918f8db..f93c2c4881d99 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.ts +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.ts @@ -1,16 +1,15 @@ 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; } diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution2.go b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution2.go deleted file mode 100644 index 3e2cd88d34bfd..0000000000000 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution2.go +++ /dev/null @@ -1,21 +0,0 @@ -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 } \ No newline at end of file