📌  相关文章
📜  长度为K的非递减子数组的数量

📅  最后修改于: 2021-04-25 04:54:55             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是找到长度为K的非递减子数组的数量。

例子:

天真的方法生成所有长度为K的子数组,然后检查子数组是否满足条件。因此,该方法的时间复杂度将为O(N * K)

更好的方法:更好的方法是使用两指针技术。假设当前索引为i

  • 找到最大的索引j ,以使子数组arr [i…j]不减小。这可以通过简单地从i + 1开始递增j的值并检查arr [j]是否大于arr [j – 1]来实现
  • 假设在上一步中找到的子数组的长度为L。其中包含的长度为K的子数组的数目将为max(L – K + 1,0)
  • 现在,更新i = j并在i处于索引范围内时重复上述步骤。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of
// increasing subarrays of length k
int cntSubArrays(int* arr, int n, int k)
{
    // To store the final result
    int res = 0;
  
    int i = 0;
    // Two pointer loop
    while (i < n) {
  
        // Initialising j
        int j = i + 1;
  
        // Looping till the subarray increases
        while (j < n and arr[j] >= arr[j - 1])
            j++;
  
        // Updating the required count
        res += max(j - i - k + 1, 0);
  
        // Updating i
        i = j;
    }
  
    // Returning res
    return res;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 2, 5 };
    int n = sizeof(arr) / sizeof(int);
    int k = 2;
  
    cout << cntSubArrays(arr, n, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the count of
// increasing subarrays of length k
static int cntSubArrays(int []arr, int n, int k)
{
    // To store the final result
    int res = 0;
  
    int i = 0;
      
    // Two pointer loop
    while (i < n)
    {
  
        // Initialising j
        int j = i + 1;
  
        // Looping till the subarray increases
        while (j < n && arr[j] >= arr[j - 1])
            j++;
  
        // Updating the required count
        res += Math.max(j - i - k + 1, 0);
  
        // Updating i
        i = j;
    }
  
    // Returning res
    return res;
}
  
// Driver code
public static void main(String []args)
{
    int arr[] = { 1, 2, 3, 2, 5 };
    int n = arr.length;
    int k = 2;
  
    System.out.println(cntSubArrays(arr, n, k));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach 
  
# Function to return the count of 
# increasing subarrays of length k 
def cntSubArrays(arr, n, k) : 
  
    # To store the final result 
    res = 0; 
  
    i = 0; 
      
    # Two pointer loop 
    while (i < n) :
  
        # Initialising j 
        j = i + 1; 
  
        # Looping till the subarray increases 
        while (j < n and arr[j] >= arr[j - 1]) :
            j += 1; 
  
        # Updating the required count 
        res += max(j - i - k + 1, 0); 
  
        # Updating i 
        i = j; 
  
    # Returning res 
    return res; 
  
# Driver code 
if __name__ == "__main__" : 
      
    arr = [ 1, 2, 3, 2, 5 ]; 
    n = len(arr); 
    k = 2; 
  
    print(cntSubArrays(arr, n, k)); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function to return the count of
// increasing subarrays of length k
static int cntSubArrays(int []arr, int n, int k)
{
    // To store the final result
    int res = 0;
  
    int i = 0;
      
    // Two pointer loop
    while (i < n)
    {
  
        // Initialising j
        int j = i + 1;
  
        // Looping till the subarray increases
        while (j < n && arr[j] >= arr[j - 1])
            j++;
  
        // Updating the required count
        res += Math.Max(j - i - k + 1, 0);
  
        // Updating i
        i = j;
    }
  
    // Returning res
    return res;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 2, 3, 2, 5 };
    int n = arr.Length;
    int k = 2;
  
    Console.WriteLine(cntSubArrays(arr, n, k));
}
}
  
// This code is contributed by Rajput-Ji


输出:
3