📜  计算非递增子数组的数量

📅  最后修改于: 2021-04-29 17:03:37             🧑  作者: Mango

给定一个由N个整数组成的数组。任务是计算不增加的子数组(大小至少为一个)的数量。

例子:

Input : arr[] = {1, 4, 3}
Output : 4
The possible subarrays are {1}, {4}, {3}, {4, 3}.

Input :{4, 3, 2, 1}
Output : 10
The possible subarrays are:
{4}, {3}, {2}, {1}, {4, 3}, {3, 2}, {2, 1}, 
{4, 3, 2}, {3, 2, 1}, {4, 3, 2, 1}.

一个简单的解决方案:一个简单的解决方案是生成所有可能的子数组,并对每个子数组检查子数组是否不增加。该解决方案的时间复杂度为O(N 3 )。

有效的解决方案:更好的解决方案是使用以下事实:如果子数组arr [i:j]不是不增加,则子数组arr [i:j + 1],arr [i:j + 2],.. arr [ i:n-1]不能不增加。因此,开始遍历数组,对于当前的子数组,请继续增加其长度,直到不增加并更新计数。一旦子阵列开始增加,就重置长度。

下面是上述想法的实现:

C++
// C++ program to count number of non
// increasing subarrays
#include 
using namespace std;
  
int countNonIncreasing(int arr[], int n)
{
    // Initialize result
    int cnt = 0;
  
    // Initialize length of current non
    // increasing subarray
    int len = 1;
  
    // Traverse through the array
    for (int i = 0; i < n - 1; ++i) {
  
        // If arr[i+1] is less than or equal to arr[i],
        // then increment length
        if (arr[i + 1] <= arr[i])
            len++;
  
        // Else Update count and reset length
        else {
            cnt += (((len + 1) * len) / 2);
            len = 1;
        }
    }
  
    // If last length is more than 1
    if (len > 1)
        cnt += (((len + 1) * len) / 2);
  
    return cnt;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 2, 3, 7, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << countNonIncreasing(arr, n);
  
    return 0;
}


Java
// Java program to count number of non
// increasing subarrays
class GFG
{
      
static int countNonIncreasing(int arr[], int n)
{
    // Initialize result
    int cnt = 0;
  
    // Initialize length of current non
    // increasing subarray
    int len = 1;
  
    // Traverse through the array
    for (int i = 0; i < n - 1; ++i)
    {
  
        // If arr[i+1] is less than or equal to arr[i],
        // then increment length
        if (arr[i + 1] <= arr[i])
            len++;
  
        // Else Update count and reset length
        else 
        {
            cnt += (((len + 1) * len) / 2);
            len = 1;
        }
    }
  
    // If last length is more than 1
    if (len > 1)
        cnt += (((len + 1) * len) / 2);
  
    return cnt;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 5, 2, 3, 7, 1, 1 };
    int n =arr.length;
  
    System.out.println(countNonIncreasing(arr, n));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 program to count number of non
# increasing subarrays
def countNonIncreasing(arr, n):
      
    # Initialize result
    cnt = 0;
  
    # Initialize length of current 
    # non-increasing subarray
    len = 1;
  
    # Traverse through the array
    for i in range(0, n - 1):
  
        # If arr[i+1] is less than
        # or equal to arr[i],
        # then increment length
        if (arr[i + 1] <= arr[i]):
            len += 1;
  
        # Else Update count and reset length
        else:
            cnt += (((len + 1) * len) / 2);
            len = 1;
              
    # If last length is more than 1
    if (len > 1):
        cnt += (((len + 1) * len) / 2);
  
    return int(cnt);
  
# Driver code
if __name__ == '__main__':
    arr = [5, 2, 3, 7, 1, 1];
    n = len(arr);
  
    print(countNonIncreasing(arr, n));
  
# This code contributed by PrinciRaj1992


C#
// C# program to count number of non
// increasing subarrays
using System;
      
class GFG
{
      
static int countNonIncreasing(int []arr, int n)
{
    // Initialize result
    int cnt = 0;
  
    // Initialize length of current non
    // increasing subarray
    int len = 1;
  
    // Traverse through the array
    for (int i = 0; i < n - 1; ++i)
    {
  
        // If arr[i+1] is less than or equal to arr[i],
        // then increment length
        if (arr[i + 1] <= arr[i])
            len++;
  
        // Else Update count and reset length
        else
        {
            cnt += (((len + 1) * len) / 2);
            len = 1;
        }
    }
  
    // If last length is more than 1
    if (len > 1)
        cnt += (((len + 1) * len) / 2);
  
    return cnt;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 5, 2, 3, 7, 1, 1 };
    int n = arr.Length;
  
    Console.Write(countNonIncreasing(arr, n));
}
}
  
// This code has been contributed by 29AjayKumar


PHP
 1)
        $cnt += (($len + 1) * $len) / 2;
  
    return $cnt;
}
  
// Driver code
$arr = array( 5, 2, 3, 7, 1, 1 );
$n = sizeof($arr);
  
echo countNonIncreasing($arr, $n);
  
// This code is contributed by akt_mit
?>


输出:
8   

时间复杂度: O(N)
辅助空间:O(1)