给定一个大小为N的数组arr[] ,两个正整数K和S ,任务是找到大小大于K的最小子数组的长度,其总和大于S 。
例子:
Input: arr[] = {1, 2, 3, 4, 5}, K = 1, S = 8
Output: 2
Explanation:
Smallest subarray with sum greater than S(=8) is {4, 5}
Therefore, the required output is 2.
Input: arr[] = {1, 3, 5, 1, 8, 2, 4}, K= 2, S= 13
Output: 3
方法:可以使用滑动窗口技术解决该问题。请按照以下步骤解决问题:
- 初始化两个变量,比如 i = 0和 j = 0都指向数组的开头,即索引 0。
- 初始化一个变量 sum 来存储当前正在处理的 subArray 的总和。
- 遍历数组arr[]并通过增加 j 并添加arr[j]
- 取我们的窗口长度或当前 subArray 的长度,它由j-i+1 (+1 因为索引从零开始) 给出。
- 首先,检查当前 subArray 的大小,即这里的 winLen 是否大于K 。如果不是这种情况,则增加 j 值并继续循环。
- 否则,我们得到当前子数组的大小大于K ,现在我们要检查是否满足第二个条件,即当前子数组的总和大于 S。
- 如果是这种情况,我们更新 minLength 变量,该变量存储满足上述条件的 subArray 的最小长度。
- 这时,我们检查子数组的大小是否可以减小(通过增加i ) 这样它仍然大于 K 并且 sum 也大于 S。我们不断地从 sum 中删除数组的第 i 个元素以减少While 循环中的 subArray 大小,然后增加j以便我们移动到下一个元素数组.the
- 最后,打印得到满足条件的所需子数组的最小长度。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the length of the
// smallest subarray of size > K with
// sum greater than S
int smallestSubarray(int K, int S,
int arr[], int N)
{
// Store the first index of
// the current subarray
int start = 0;
// Store the last index of
// the the current subarray
int end = 0;
// Store the sum of the
// current subarray
int currSum = arr[0];
// Store the length of
// the smallest subarray
int res = INT_MAX;
while (end < N - 1) {
// If sum of the current subarray <= S
// or length of current subarray <= K
if (currSum <= S
|| (end - start + 1) <= K) {
// Increase the subarray
// sum and size
currSum += arr[++end];
}
// Otherwise
else {
// Update to store the minimum
// size of subarray obtained
res = min(res, end - start + 1);
// Decrement current subarray
// size by removing first element
currSum -= arr[start++];
}
}
// Check if it is possible to reduce
// the length of the current window
while (start < N) {
if (currSum > S
&& (end - start + 1) > K)
res = min(res, (end - start + 1));
currSum -= arr[start++];
}
return res;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int K = 1, S = 8;
int N = sizeof(arr) / sizeof(arr[0]);
cout << smallestSubarray(K, S, arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to find the length of the
// smallest subarray of size > K with
// sum greater than S
public static int smallestSubarray(int k, int s,
int[] array, int N)
{
int i=0;
int j=0;
int minLen = Integer.MAX_VALUE;
int sum = 0;
while(j < N)
{
sum += array[j];
int winLen = j-i+1;
if(winLen <= k)
j++;
else{
if(sum > s)
{
minLen = Math.min(minLen,winLen);
while(sum > s)
{
sum -= array[i];
i++;
}
j++;
}
}
}
return minLen;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
int K = 1, S = 8;
int N = arr.length;
System.out.print(smallestSubarray(K, S, arr, N));
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program to implement
# the above approach
import sys
# Function to find the length of the
# smallest subarray of size > K with
# sum greater than S
def smallestSubarray(K, S, arr, N):
# Store the first index of
# the current subarray
start = 0
# Store the last index of
# the the current subarray
end = 0
# Store the sum of the
# current subarray
currSum = arr[0]
# Store the length of
# the smallest subarray
res = sys.maxsize
while end < N - 1:
# If sum of the current subarray <= S
# or length of current subarray <= K
if ((currSum <= S) or
((end - start + 1) <= K)):
# Increase the subarray
# sum and size
end = end + 1;
currSum += arr[end]
# Otherwise
else:
# Update to store the minimum
# size of subarray obtained
res = min(res, end - start + 1)
# Decrement current subarray
# size by removing first element
currSum -= arr[start]
start = start + 1
# Check if it is possible to reduce
# the length of the current window
while start < N:
if ((currSum > S) and
((end - start + 1) > K)):
res = min(res, (end - start + 1))
currSum -= arr[start]
start = start + 1
return res;
# Driver Code
if __name__ == "__main__":
arr = [ 1, 2, 3, 4, 5 ]
K = 1
S = 8
N = len(arr)
print(smallestSubarray(K, S, arr, N))
# This code is contributed by akhilsaini
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the length of the
// smallest subarray of size > K with
// sum greater than S
static int smallestSubarray(int K, int S,
int[] arr, int N)
{
// Store the first index of
// the current subarray
int start = 0;
// Store the last index of
// the the current subarray
int end = 0;
// Store the sum of the
// current subarray
int currSum = arr[0];
// Store the length of
// the smallest subarray
int res = int.MaxValue;
while (end < N - 1)
{
// If sum of the current subarray <= S
// or length of current subarray <= K
if (currSum <= S ||
(end - start + 1) <= K)
{
// Increase the subarray
// sum and size
currSum += arr[++end];
}
// Otherwise
else
{
// Update to store the minimum
// size of subarray obtained
res = Math.Min(res, end - start + 1);
// Decrement current subarray
// size by removing first element
currSum -= arr[start++];
}
}
// Check if it is possible to reduce
// the length of the current window
while (start < N)
{
if (currSum > S && (end - start + 1) > K)
res = Math.Min(res, (end - start + 1));
currSum -= arr[start++];
}
return res;
}
// Driver Code
static public void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
int K = 1, S = 8;
int N = arr.Length;
Console.Write(smallestSubarray(K, S, arr, N));
}
}
// This code is contributed by akhilsaini
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live