返回介绍

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

发布于 2024-06-17 01:03:08 字数 5467 浏览 0 评论 0 收藏 0

2208. Minimum Operations to Halve Array Sum

中文文档

Description

You are given an array nums of positive integers. In one operation, you can choose any number from nums and reduce it to exactly half the number. (Note that you may choose this reduced number in future operations.)

Return_ the minimum number of operations to reduce the sum of _nums_ by at least half._

 

Example 1:

Input: nums = [5,19,8,1]
Output: 3
Explanation: The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33.
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 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.

Example 2:

Input: nums = [3,8,20]
Output: 3
Explanation: The initial sum of nums is equal to 3 + 8 + 20 = 31.
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 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.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 107

Solutions

Solution 1

class Solution:
  def halveArray(self, nums: List[int]) -> int:
    s = sum(nums) / 2
    h = []
    for v in nums:
      heappush(h, -v)
    ans = 0
    while s > 0:
      t = -heappop(h) / 2
      s -= t
      heappush(h, -t)
      ans += 1
    return ans
class Solution {
  public int halveArray(int[] nums) {
    double s = 0;
    PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
    for (int v : nums) {
      q.offer(v * 1.0);
      s += v;
    }
    s /= 2.0;
    int ans = 0;
    while (s > 0) {
      double t = q.poll();
      s -= t / 2.0;
      q.offer(t / 2.0);
      ++ans;
    }
    return ans;
  }
}
class Solution {
public:
  int halveArray(vector<int>& nums) {
    priority_queue<double> q;
    double s = 0;
    for (int& v : nums) {
      s += v;
      q.push(v);
    }
    s /= 2.0;
    int ans = 0;
    while (s > 0) {
      double t = q.top() / 2;
      q.pop();
      s -= t;
      q.push(t);
      ++ans;
    }
    return ans;
  }
};
func halveArray(nums []int) (ans int) {
  var s float64
  q := hp{}
  for _, x := range nums {
    s += float64(x)
    heap.Push(&q, float64(x))
  }
  s /= 2
  for s > 0 {
    x := heap.Pop(&q).(float64)
    ans++
    s -= x / 2
    heap.Push(&q, x/2)
  }
  return
}

type hp struct{ sort.Float64Slice }

func (h hp) Less(i, j int) bool { return h.Float64Slice[i] > h.Float64Slice[j] }
func (h *hp) Push(v any)    { h.Float64Slice = append(h.Float64Slice, v.(float64)) }
func (h *hp) Pop() any {
  a := h.Float64Slice
  v := a[len(a)-1]
  h.Float64Slice = a[:len(a)-1]
  return v
}
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);
  }
  let ans: number = 0;
  while (s > 0) {
    let { element: t } = h.dequeue();
    t /= 2;
    s -= t;
    h.enqueue(t, t);
    ans += 1;
  }
  return ans;
}

Solution 2

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 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文