给定一个大小为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
方法:可以使用滑动窗口技术解决该问题。请按照以下步骤解决问题:
- 初始化两个变量,例如start = 0和end = 0 ,分别存储当前子数组的第一个和最后一个索引。
- 遍历数组arr []并通过增加end并将arr [end]添加到当前子数组的总和中来。
- 如果当前子数组的所有元素的总和小于或等于S或当前子数组的长度小于或等于K ,则增加当前子数组的长度( end ++ )。
- 否则,通过删除当前子数组的第一个元素来减少当前子数组的长度(start- = 1 )。通过将其与当前子数组的长度进行比较,来更新到目前为止找到的所需子数组的最小长度。
- 最后,打印满足条件的所需子数组的最小长度。
下面是上述方法的实现:
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[] 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 = Integer.MAX_VALUE;
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
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)