📜  长度大于或等于K的非递减子数组的数量

📅  最后修改于: 2021-04-26 18:28:40             🧑  作者: Mango

给定N个元素的数组arr []和整数K ,任务是查找长度大于或等于K的非递减子数组的数量。

例子:

天真的方法:一种简单的方法是生成所有长度大于或等于K的子数组,然后检查子数组是否满足条件。因此,该方法的时间复杂度将为O(N 3 )

高效的方法:更好的方法是使用两指针技术。

  • 对于任何索引i ,找到最大索引j ,以使子数组arr [i…j]不变。这可以通过简单地从i + 1开始增加j的值并检查arr [j]是否大于arr [j – 1]来实现
  • 假设在上一步中找到的子数组的长度为L。计算X = max(0,L – K + 1)(X *(X + 1))/ 2将添加到最终答案中。这是因为对于长度为L的数组,长度为K的子数组的数目。
    • 从第一个元素开始的此类子数组的数量= L – K + 1 =X
    • 从第二个元素开始的此类子数组的数量= L – K = X – 1
    • 从第三个元素= L – K – 1 = X – 2开始的子数组的数量。
    • 依此类推,直到0,即1 + 2 + 3 + .. + X =(X *(X + 1))/ 2

下面是上述方法的实现:

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


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


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


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


输出:
0