And the shortest sub-array of at least K

Returns the length of continuous non-empty sub-array A is the shortest, and the sub-array of at least K

If no array and K is at least a non-empty, return -1.

Example 1:

Input: A = [1], K = 1
Output: 1

Example 2:

Input: A = [1,2], K = 4
Output: -1

Example 3:

Input: A = [2, -1,2] , K = 3
Output: 3

1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9

 

 

The key to this question we need to know for each section and. Thereby interval and to determine which requirements are satisfied. Method for determining individually with profits are possible, but too time-consuming. Because violent solution actually do a lot of repetitive work. So in order to save repetitive calculations, there is a way to save is to prefix and an array of arrays. It is to use an array to record the sum of the individual sections and. That is

sum [i] = arr [0] + arr [1] + ...... + arr [i-1]. For convenience calculation that increasing the sum [0] = 0. With this array, we can easily solve the arbitrary intervals and.

Such as requiring the interval [4,6] and it can sum [6] -sum [4].

 

Then the next job is the summation of each value you want to sum the array into the queue.

(1) When the value of the interval [0, i] is larger than a value [0, i-1] of the time, directly into the queue.

(2) When the interval [0 ... i] a value greater than the interval [0 ... .i-1] also small, so the position for the latter, in fact, can be ignored i-1, since the sum [n] -sum [ i]> sum [n] -sum [n-1]. I.e. [i, n] value interval ratio [i-1, n] value is larger, but shorter. Thus the position i-1 can be kicked out of the queue.

Queue inside such values ​​as follows: 0,1,3,5,4. Because 4 to 5 hours, 5 at this time should be removed out of the queue, why, if the reservation queue 5 in which, when such an array 7 back into the queue when the queue becomes 0,1,3,5,4 array, 7. This time interval is calculated and is significantly smaller than 7-5 7-4. And [5,7] interval of length 2, [4,7] interval of length 1. 5 thus is not necessary in the queue. The value of the team was changed to 0,1,3,4,7

 

After each queue insertion operation is completed, begin to queue inside the interval is calculated.

When the queue time is 0, 1, 1-0 <4 is not satisfied

When the queue time is 0,1,3, 3-1 <4 is not satisfied

When the queue time is 0,1,3,5, 5-0> 4 satisfies the condition, the minimum length of 3.0 queue, the queue becomes 1,3,5 continue Comparative 5-1> 4, the minimum length is 2 , a queue, the queue becomes 3,5.

When the queue is 3,4 time (due to the 5 to 4 hours, so the queue), 4-3 <4 is not satisfied

When the queue time is 3,4,7, 7-3> = 4, the condition, the minimum length is 2

 

code show as below:

No cohort or stack structure, it is an array to store dquue, with dq_begin, dq_end, dq_size to represent a first respective array, and the last element of the array.

int shortestSubarray(int a[], int k, int len)

{

    int * dqueue;

    int *sum;

    int i,dq_begin,dq_end,dq_size,min_length,lengh;

    dqueue = (int *)malloc((len + 1) * sizeof(int));

    sum = (int *)malloc(len * sizeof(int));

    memset(sum, 0,len * sizeof(int));

    memset(dqueue, 0, (len + 1) * sizeof(int));

    dq_begin = 0;

    dq_end = -1;

    dq_size = 0;

    min_length = len+1;

    for (i = 1; i <= len; i++)

    {

        *(sum + i) = *(sum+i-1)+a[i-1];

    }

    for (i = 0; i <= len; i++)

    {

        if (i != 0)

        {

             while (dq_size > 0 && sum[dq_end] >= sum[i])

             {

                 dq_end -= 1;

dq_size -= 1;

             }

             while (dq_size > 0 && sum[i] - sum[dq_begin] >= k)

             {

                 lengh = i - dq_begin;

                 min_length = min_length >= lengh ? lengh : min_length;

                 dq_begin += 1;

                 dq_size -= 1;

             }

        }

        dq_end += 1;

        dqueue[dq_end] = i;

        dq_size += 1;

    }

    return min_length;

 

}

 

Guess you like

Origin www.cnblogs.com/zhanghongfeng/p/11621746.html