Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 大于密钥的最小子数组_Arrays_Algorithm_Computer Science - Fatal编程技术网

Arrays 大于密钥的最小子数组

Arrays 大于密钥的最小子数组,arrays,algorithm,computer-science,Arrays,Algorithm,Computer Science,我有一个整数数组(不一定排序),我想找到一个连续的子数组,它的值之和最小,但大于一个特定的值K e、 g: 输入:数组:{1,2,4,9,5},键值:10 输出:{4,9} 我知道在O(n^2)中很容易做到这一点,但我想在O(n) 我的想法是:我无法在O(n)中找到这一点,但我所能想到的只是O(n^2)时间复杂性 让我们假设它只能有正值 那就容易了 该解决方案是最小(最短)连续子阵列之一,其和为>K 取两个索引,一个用于子阵列的开始,一个用于结束(一个超过结束),从end=0和start=0开始

我有一个整数数组(不一定排序),我想找到一个连续的子数组,它的值之和最小,但大于一个特定的值
K

e、 g:

输入:数组:
{1,2,4,9,5}
,键值:
10

输出:
{4,9}

我知道在
O(n^2)
中很容易做到这一点,但我想在
O(n)

我的想法是:我无法在
O(n)
中找到这一点,但我所能想到的只是
O(n^2)
时间复杂性


让我们假设它只能有正值

那就容易了

该解决方案是最小(最短)连续子阵列之一,其和为
>K

取两个索引,一个用于子阵列的开始,一个用于结束(一个超过结束),从
end=0
start=0
开始。初始化
sum=0
min=无穷大

while(end < arrayLength) {
    while(end < arrayLength && sum <= K) {
        sum += array[end];
        ++end;
    }
    // Now you have a contiguous subarray with sum > K, or end is past the end of the array
    while(sum - array[start] > K) {
        sum -= array[start];
        ++start;
    }
    // Now, you have a _minimal_ contiguous subarray with sum > K (or end is past the end)
    if (sum > K && sum < min) {
        min = sum;
        // store start and end if desired
    }
    // remove first element of the subarray, so that the next round begins with
    // an array whose sum is <= K, for the end index to be increased
    sum -= array[start];
    ++start;
}
while(endK){
总和-=数组[开始];
++开始;
}
//现在,您有了一个_minimal u连续的子数组,其和>K(或者end超过了end)
如果(总和>K&&总和<最小值){
最小值=总和;
//如果需要,存储开始和结束
}
//移除子阵列的第一个元素,以便下一轮以

//一个数组,其总和为正负数(不完全确定负数)的Java实现,它在O(n)时间和O(1)空间中工作

public static int find subsequencewith minimum sumer大于给定值(int[]数组,int n){
if(null==数组){
返回-1;
}
int minSum=0;
int currentSum=0;
布尔值isSumFound=false;
int startIndex=0;
for(int i=0;i=n){
while(currentSum-array[startIndex]>=n){
currentSum-=数组[startIndex];
startIndex++;
}
isSumFound=true;
minSum=电流总和;
}
}否则{
currentSum+=数组[i];
int tempSum=电流总和;
如果(tempSum>=n){
while(tempSum-array[startIndex]>=n){
tempSum-=数组[startIndex];
startIndex++;
}
if(tempSum临时总数){
minSum=tempSum;
}
currentSum=tempSum;
}
}否则{
继续;
}
}
}
System.out.println(“startIndex:+startIndex”);
返回minSum;
}

数组可以有负元素,还是只有非负元素?假设它只能有正值。
public static int findSubSequenceWithMinimumSumGreaterThanGivenValue(int[] array, int n) {

    if (null == array) {
        return -1;
    }

    int minSum = 0;
    int currentSum = 0;
    boolean isSumFound = false;
    int startIndex = 0;
    for (int i = 0; i < array.length; i++) {
        if (!isSumFound) {
            currentSum += array[i];
            if (currentSum >= n) {
                while (currentSum - array[startIndex] >= n) {
                    currentSum -= array[startIndex];
                    startIndex++;
                }
                isSumFound = true;
                minSum = currentSum;
            }
        } else {
            currentSum += array[i];
            int tempSum = currentSum;
            if (tempSum >= n) {
                while (tempSum - array[startIndex] >= n) {
                    tempSum -= array[startIndex];
                    startIndex++;
                }
                if (tempSum < currentSum) {
                    if (minSum > tempSum) {
                        minSum = tempSum;
                    }
                    currentSum = tempSum;
                }
            } else {
                continue;
            }
        }
    }
    System.out.println("startIndex:" + startIndex);
    return minSum;
}