📜  查找第 K 个最大和的连续子数组的Java程序

📅  最后修改于: 2022-05-13 01:55:44.716000             🧑  作者: Mango

查找第 K 个最大和的连续子数组的Java程序

给定一个整数数组。编写一个程序,在具有负数和正数的数字数组中找到连续子数组的第 K 个最大和。

例子:

Input: a[] = {20, -5, -1} 
         k = 3
Output: 14
Explanation: All sum of contiguous 
subarrays are (20, 15, 14, -5, -6, -1) 
so the 3rd largest sum is 14.

Input: a[] = {10, -10, 20, -40} 
         k = 6
Output: -10 
Explanation: The 6th largest sum among 
sum of all contiguous subarrays is -10.

蛮力方法是将所有连续的和存储在另一个数组中,然后对其进行排序并打印第 k 个最大的值。但是在元素数量很大的情况下,我们存储连续和的数组将耗尽内存,因为连续子数组的数量会很大(二次顺序)

一种有效的方法是将数组的预和存储在 sum[] 数组中。我们可以找到从索引 i 到 j 的连续子数组的总和为 sum[j]-sum[i-1]

现在为了存储第 K 个最大的和,使用一个最小堆(优先队列),我们在其中推送连续的和直到我们得到 K 个元素,一旦我们有了 K 个元素,检查元素是否大于它插入的第 K 个元素弹出最小堆中顶部元素的最小堆,否则不插入。最后,最小堆中的顶部元素将是您的答案。

下面是上述方法的实现。

Java
// Java program to find the k-th
// largest sum of subarray
import java.util.*;
 
class KthLargestSumSubArray
{
    // function to calculate kth largest
    // element in contiguous subarray sum
    static int kthLargestSum(int arr[], int n, int k)
    {
        // array to store predix sums
        int sum[] = new int[n + 1];
        sum[0] = 0;
        sum[1] = arr[0];
        for (int i = 2; i <= n; i++)
            sum[i] = sum[i - 1] + arr[i - 1];
         
        // priority_queue of min heap
        PriorityQueue Q = new PriorityQueue ();
         
        // loop to calculate the contiguous subarray
        // sum position-wise
        for (int i = 1; i <= n; i++)
        {
     
            // loop to traverse all positions that
            // form contiguous subarray
            for (int j = i; j <= n; j++)
            {
                // calculates the contiguous subarray
                // sum from j to i index
                int x = sum[j] - sum[i - 1];
     
                // if queue has less then k elements,
                // then simply push it
                if (Q.size() < k)
                    Q.add(x);
     
                else
                {
                    // it the min heap has equal to
                    // k elements then just check
                    // if the largest kth element is
                    // smaller than x then insert
                    // else its of no use
                    if (Q.peek() < x)
                    {
                        Q.poll();
                        Q.add(x);
                    }
                }
            }
        }
         
        // the top element will be then kth
        // largest element
        return Q.poll();
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int a[] = new int[]{ 10, -10, 20, -40 };
        int n = a.length;
        int k = 6;
 
        // calls the function to find out the
        // k-th largest sum
        System.out.println(kthLargestSum(a, n, k));
    }
}
 
/* This code is contributed by Danish Kaleem */


输出:

-10

时间复杂度: O(n^2 log (k))
辅助空间:最小堆的 O(k),我们可以将 sum 数组存储在数组本身中,因为它没有用。
有关详细信息,请参阅有关 K-th Largest Sum Contiguous Subarray 的完整文章!