总和大于给定值的子数组数

编程入门 行业动态 更新时间:2024-10-13 06:16:47
本文介绍了总和大于给定值的子数组数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给出一个由 N 个整数组成的数组(正数和负数),找到其总和大于或等于 K的连续个子数组的数量(也,正或负)

Given an array of N integers (positive and negative), find the number of contiguous sub array whose sum is greater or equal to K (also, positive or negative)

我设法解决了一个幼稚的O(N 2 )解决方案,有可能变得更好吗?

I have managed to work out a naive O(N2) solution, is it possible to get better?

推荐答案

是的,可以在O(n log n)时间完成.

Yes, it is possible to do it in O(n log n) time.

  • 让我们看一下前缀总和. (L, R]子数组的总和为prefixSum[R] - prefixSum[L].

    这意味着我们可以计算L < R和prefixSum[R] - prefixSum[L] >= K表示prefixSum[L] <= prefixSum[R] - K的L和R的数量.

    It means that we can count the number of such L and R that L < R and prefixSum[R] - prefixSum[L] >= K, which means prefixSum[L] <= prefixSum[R] - K.

    让我们从左到右遍历前缀和数组,并维护一个可以有效执行以下操作的数据结构:

    Let's iterate over the array of prefix sums from left to right and maintain a data structure that can do the following operations efficiently :

    • 添加新元素.
    • 查找小于或等于给定数目的元素数.

    为此,我们可以使用平衡的二叉搜索树.

    We can use a balanced binary search tree for this purpose.

    这是此解决方案的伪代码:

    Here is a pseudo-code of this solution:

    tree = an empty search tree result = 0 // This sum corresponds to an empty prefix. prefixSum = 0 tree.add(prefixSum) // Iterate over the input array from left to right. for elem <- array: prefixSum += elem // Add the number of subarrays that have this element as the last one // and their sum is not less than K. result += tree.getNumberOfLessOrEqual(prefixSum - K) // Add the current prefix sum the tree. tree.add(prefixSum) print result

    时间复杂度为O(n log n),因为有O(n)个元素进行加法和计数,并且每个操作都可以在O(log n)中完成.

    The time complexity is O(n log n) because there are O(n) add and count number of elements operations, and each of them can be done in O(log n).

  • 更多推荐

    总和大于给定值的子数组数

    本文发布于:2023-11-30 00:08:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648061.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数组   总和   定值

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!