计算具有严格递减连续元素的子数组
给定一个包含整数的数组arr[] 。任务是找到差为1的递减子数组的数量。
例子:
Input: arr[] = {3, 2, 1, 4}
Output: 7
Explanation: Following are the possible decreasing subarrays with difference 1.
[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]
Therefore, the answer is 7.
Input: arr[] = {5, 4, 3, 2, 1, 6}
Output: 16
朴素的方法:这个问题可以通过使用动态规划来解决。请按照以下步骤解决给定的问题。
- 对于每个索引 i 的任务是计算以i结尾的子数组的数量,它遵循这种模式arr[i-2]==arr[i-1]+1 , arr[i-1]==arr[i]+1 .
- 初始化一个变量ans = 0 ,以存储差值为1的递减子数组的数量。
- 我们可以创建一个dp[]数组来存储每个索引的这些连续元素的计数。
- dp[i] 是以i结尾且遵循此模式的子数组的数量。
- 遍历dp[]并将 ans 中的每个值相加。
- 返回ans作为最终结果。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to count number of
// decreasing subarrays with difference 1
long long getcount(vector& p)
{
int size = p.size(), cnt = 0;
long long ans = 0;
vector dp(size, cnt);
for (int i = 0; i < size; i++) {
if (i == 0)
cnt = 1;
else if (p[i] + 1 == p[i - 1])
cnt++;
else
cnt = 1;
dp[i] = cnt;
}
for (int i = 0; i < size; i++)
ans += dp[i];
return ans;
}
// Driver Code
int main()
{
vector arr{ 5, 4, 3, 2, 1, 6 };
// Function Call
cout << getcount(arr);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
public class GFG
{
// Function to count number of
// decreasing subarrays with difference 1
static long getcount(int p[])
{
int size = p.length, cnt = 0;
long ans = 0;
int dp[] = new int[size];
for(int i = 0; i < size; i++) {
dp[i] = cnt;
}
for (int i = 0; i < size; i++) {
if (i == 0)
cnt = 1;
else if (p[i] + 1 == p[i - 1])
cnt++;
else
cnt = 1;
dp[i] = cnt;
}
for (int i = 0; i < size; i++)
ans += dp[i];
return ans;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 5, 4, 3, 2, 1, 6 };
// Function Call
System.out.println(getcount(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code to implement the above approach
# Function to count number of
# decreasing subarrays with difference 1
def getcount(p):
size = len(p)
cnt = 0
ans = 0
dp = [cnt for i in range(size)]
for i in range(size):
if (i == 0):
cnt = 1
elif (p[i] + 1 == p[i - 1]):
cnt += 1
else:
cnt = 1
dp[i] = cnt
for i in range(size):
ans += dp[i]
return ans
# Driver Code
arr = [5, 4, 3, 2, 1, 6]
# Function Call
print(getcount(arr))
# This code is contributed by Shubham Singh
C#
// C# code to implement the above approach
using System;
class GFG
{
// Function to count number of
// decreasing subarrays with difference 1
static long getcount(int []p)
{
int size = p.Length, cnt = 0;
long ans = 0;
int []dp = new int[size];
for(int i = 0; i < size; i++) {
dp[i] = cnt;
}
for (int i = 0; i < size; i++) {
if (i == 0)
cnt = 1;
else if (p[i] + 1 == p[i - 1])
cnt++;
else
cnt = 1;
dp[i] = cnt;
}
for (int i = 0; i < size; i++)
ans += dp[i];
return ans;
}
// Driver code
public static void Main()
{
int []arr = { 5, 4, 3, 2, 1, 6 };
// Function Call
Console.Write(getcount(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++
// C++ program for above approach
#include
using namespace std;
// Function to count the number of
// decreasing subarrays with difference 1
long long getcount(vector& arr)
{
long long int ans = arr.size();
long long int count = 0;
for (int i = 1; i < arr.size(); i++) {
if (arr[i - 1] - arr[i] == 1)
count++;
else
count = 0;
ans = ans + count;
}
return ans;
}
// Driver Code
int main()
{
vector arr{ 5, 4, 3, 2, 1, 6 };
// Function Call
cout << getcount(arr);
return 0;
}
Java
// Java program for above approach
class GFG
{
// Function to count the number of
// decreasing subarrays with difference 1
static long getcount(int[] arr)
{
int ans = arr.length;
int count = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] - arr[i] == 1)
count++;
else
count = 0;
ans = ans + count;
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 5, 4, 3, 2, 1, 6 };
// Function Call
System.out.print(getcount(arr));
}
}
// This code is contributed by 29AjayKumar
Python3
#Python program for the above approach
# Function to count the number of
# decreasing subarrays with difference 1
def getcount(arr):
ans = len(arr)
count = 0
for i in range(1, len(arr)):
if (arr[i - 1] - arr[i] == 1):
count+=1
else:
count = 0
ans = ans + count
return ans
# Driver Code
arr = [ 5, 4, 3, 2, 1, 6 ]
# Function Call
print(getcount(arr))
# This code is contributed by Shubham Singh
C#
// C# program for above approach
using System;
public class GFG
{
// Function to count the number of
// decreasing subarrays with difference 1
static long getcount(int[] arr)
{
int ans = arr.Length;
int count = 0;
for (int i = 1; i < arr.Length; i++) {
if (arr[i - 1] - arr[i] == 1)
count++;
else
count = 0;
ans = ans + count;
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 5, 4, 3, 2, 1, 6 };
// Function Call
Console.Write(getcount(arr));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
16
时间复杂度: O(N)
辅助空间: O(N)
高效方法:在上述方法中,辅助空间复杂度可以通过用变量替换dp[]数组来进一步优化到恒定空间,以跟踪当前子数组的数量。请按照以下步骤解决给定的问题。
- 初始化一个变量说count = 0 。
- 当arr[i]-arr[i-1 ]==1开始遍历数组,生成一个递减1的数字链,然后count++ 。
- 将计数添加到 ans。
- 当链中断时, arr[i]-arr[i-1] !=1然后重置计数。
- 返回ans作为最终结果。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to count the number of
// decreasing subarrays with difference 1
long long getcount(vector& arr)
{
long long int ans = arr.size();
long long int count = 0;
for (int i = 1; i < arr.size(); i++) {
if (arr[i - 1] - arr[i] == 1)
count++;
else
count = 0;
ans = ans + count;
}
return ans;
}
// Driver Code
int main()
{
vector arr{ 5, 4, 3, 2, 1, 6 };
// Function Call
cout << getcount(arr);
return 0;
}
Java
// Java program for above approach
class GFG
{
// Function to count the number of
// decreasing subarrays with difference 1
static long getcount(int[] arr)
{
int ans = arr.length;
int count = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] - arr[i] == 1)
count++;
else
count = 0;
ans = ans + count;
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 5, 4, 3, 2, 1, 6 };
// Function Call
System.out.print(getcount(arr));
}
}
// This code is contributed by 29AjayKumar
Python3
#Python program for the above approach
# Function to count the number of
# decreasing subarrays with difference 1
def getcount(arr):
ans = len(arr)
count = 0
for i in range(1, len(arr)):
if (arr[i - 1] - arr[i] == 1):
count+=1
else:
count = 0
ans = ans + count
return ans
# Driver Code
arr = [ 5, 4, 3, 2, 1, 6 ]
# Function Call
print(getcount(arr))
# This code is contributed by Shubham Singh
C#
// C# program for above approach
using System;
public class GFG
{
// Function to count the number of
// decreasing subarrays with difference 1
static long getcount(int[] arr)
{
int ans = arr.Length;
int count = 0;
for (int i = 1; i < arr.Length; i++) {
if (arr[i - 1] - arr[i] == 1)
count++;
else
count = 0;
ans = ans + count;
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 5, 4, 3, 2, 1, 6 };
// Function Call
Console.Write(getcount(arr));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
16
时间复杂度: O(N)
辅助空间: O(1)