返回介绍

solution / 2500-2599 / 2599.Make the Prefix Sum Non-negative / README_EN

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

2599. Make the Prefix Sum Non-negative

中文文档

Description

You are given a 0-indexed integer array nums. You can apply the following operation any number of times:

  • Pick any element from nums and put it at the end of nums.

The prefix sum array of nums is an array prefix of the same length as nums such that prefix[i] is the sum of all the integers nums[j] where j is in the inclusive range [0, i].

Return _the minimum number of operations such that the prefix sum array does not contain negative integers_. The test cases are generated such that it is always possible to make the prefix sum array non-negative.

 

Example 1:

Input: nums = [2,3,-5,4]
Output: 0
Explanation: we do not need to do any operations.
The array is [2,3,-5,4]. The prefix sum array is [2, 5, 0, 4].

Example 2:

Input: nums = [3,-5,-2,6]
Output: 1
Explanation: we can do one operation on index 1.
The array after the operation is [3,-2,6,-5]. The prefix sum array is [3, 1, 7, 2].

 

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Solutions

Solution 1: Greedy + Priority Queue (Min Heap)

We use a variable $s$ to record the prefix sum of the current array.

Traverse the array $nums$, add the current element $x$ to the prefix sum $s$. If $x$ is a negative number, add $x$ to the min heap. If $s$ is negative at this time, greedily take out the smallest negative number and subtract it from $s$, and add one to the answer. Finally, return the answer.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.

class Solution:
  def makePrefSumNonNegative(self, nums: List[int]) -> int:
    h = []
    ans = s = 0
    for x in nums:
      s += x
      if x < 0:
        heappush(h, x)
      while s < 0:
        s -= heappop(h)
        ans += 1
    return ans
class Solution {
  public int makePrefSumNonNegative(int[] nums) {
    PriorityQueue<Integer> pq = new PriorityQueue<>();
    int ans = 0;
    long s = 0;
    for (int x : nums) {
      s += x;
      if (x < 0) {
        pq.offer(x);
      }
      while (s < 0) {
        s -= pq.poll();
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int makePrefSumNonNegative(vector<int>& nums) {
    priority_queue<int, vector<int>, greater<int>> pq;
    int ans = 0;
    long long s = 0;
    for (int& x : nums) {
      s += x;
      if (x < 0) {
        pq.push(x);
      }
      while (s < 0) {
        s -= pq.top();
        pq.pop();
        ++ans;
      }
    }
    return ans;
  }
};
func makePrefSumNonNegative(nums []int) (ans int) {
  pq := hp{}
  s := 0
  for _, x := range nums {
    s += x
    if x < 0 {
      heap.Push(&pq, x)
    }
    for s < 0 {
      s -= heap.Pop(&pq).(int)
      ans++
    }
  }
  return ans
}

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Push(v any)    { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
  a := h.IntSlice
  v := a[len(a)-1]
  h.IntSlice = a[:len(a)-1]
  return v
}
function makePrefSumNonNegative(nums: number[]): number {
  const pq = new MinPriorityQueue();
  let ans = 0;
  let s = 0;
  for (const x of nums) {
    s += x;
    if (x < 0) {
      pq.enqueue(x);
    }
    while (s < 0) {
      s -= pq.dequeue().element;
      ++ans;
    }
  }
  return ans;
}

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

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

发布评论

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