如何找到最小k长度和最大总和的子阵列?

时间:2012-11-02 04:27:07

标签: c++ algorithm dynamic-programming kadanes-algorithm

子阵列包含正数和负数。你必须找到一个最大和子数组,使子数组的长度大于或等于k。

这是我使用Kadane算法的c ++代码。

#include <iostream>

using namespace std;

int main(){

    int n,k;
    cin >> n >> k;
    int array[n];
    int sum = 0;
    int maxsum = 0;
    int beststarts[n];

    for(int i = 0;i < n; i++){
            cin >> array[i];
    }

    for(int i = 0;i < k-1;i ++){
            sum = sum+array[i];
            beststarts[i] = 0;
    }


    for(int i =  k-1;i < n; i++){ //best end search with min length;
            sum = sum+array[i];
            int testsum = sum;
            if(i > 0){
            beststarts[i] = beststarts[i-1];
            }
            for(int j = beststarts[i] ;i-j > k-1;j ++){
                    testsum = testsum - array[j];
                    if(testsum > sum){
                            beststarts[i] = j+1;
                            sum = testsum;
                    }
            }
            if(sum > maxsum){
                    maxsum = sum;
            }
    }

    cout << maxsum;

    return 0;
}

我的代码运行正常,但速度非常慢,我无法想到任何改进代码的方法。我也读过这个问题Find longest subarray whose sum divisible by K,但这不是我想要的,长度也可以大于k。

2 个答案:

答案 0 :(得分:3)

基于this answer

的解决方案

Live demo

#include <algorithm>
#include <iterator>
#include <iostream>
#include <numeric>
#include <ostream>
#include <utility>
#include <vector>

// __________________________________________________

template<typename RandomAccessIterator> typename std::iterator_traits<RandomAccessIterator>::value_type
max_subarr_k(RandomAccessIterator first,RandomAccessIterator last,int k)
{
    using namespace std;
    typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
    if(distance(first,last) < k)
        return value_type(0);
    RandomAccessIterator tail=first;
    first+=k;
    value_type window=accumulate(tail,first,value_type(0));
    value_type max_sum=window, current_sum=window;
    while(first!=last)
    {
        window += (*first)-(*tail) ;
        current_sum = max( current_sum+(*first), window );
        max_sum = max(max_sum,current_sum);
        ++first;
        ++tail;
    }
    return max_sum;
}

// __________________________________________________

template<typename E,int N>
E *end(E (&arr)[N])
{
    return arr+N;
}

int main()
{
    using namespace std;
    int arr[]={1,2,4,-5,-4,-3,2,1,5,6,-20,1,1,1,1,1};
    cout << max_subarr_k(arr,end(arr),4) << endl;
    cout << max_subarr_k(arr,end(arr),5) << endl;
}

输出是:

14
11

答案 1 :(得分:0)

  int w(0);
    for (int i=0; i < k; i++) w += a[i];
    int run_sum(w), max_sum(w);
    for (int i=k; i < n; i++) {
              w = a[i] + max(w, w-a[i-k]); //  window will such that it will include run_sum
              run_sum = max(run_sum + a[i], w);
              max_sum = max(run_sum, max_sum); 
    }
    return max_sum;