给定一个由整数[1, N ]组成的数组A[] ,任务是计算所有可能长度x ( 1 ≤ x ≤ N ) 的子数组的总数,由整数[1, x]的排列组成给定的数组。
例子:
Input: A[] = {3, 1, 2, 5, 4} Output: 4
Explanation:
Subarrays forming a permutation are {1}, {1, 2}, {3, 1, 2} and {3, 1, 2, 5, 4}.
Input: A[] = {4, 5, 1, 3, 2, 6} Output: 4
Explanation:
Subarrays forming a permutation are {1}, {1, 3, 2}, {4, 5, 1, 3, 2} and {4, 5, 1, 3, 2, 6}.
天真的方法:
请按照以下步骤解决问题:
- 解决问题的最简单方法是生成所有可能的子数组。
- 对于每个子数组,检查它是否是[1, length of subarray]范围内元素的排列。
- 对于找到的每个这样的子数组,增加count 。最后,打印计数。
时间复杂度: O(N 3 )
辅助空间: O(1)
有效的方法:
要优化上述方法,请按照以下步骤操作:
- 对于i = [1, N] 中的每个元素,检查最大和最小 index ,其中存在排列[1, i]的元素。
- 如果最大和最小索引之间的差等于i ,则意味着i存在有效的连续排列。
- 对于每个这样的排列,增加计数。最后,打印计数。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function returns the required count
int PermuteTheArray(int A[], int n)
{
int arr[n];
// Store the indices of the
// elements present in A[].
for (int i = 0; i < n; i++) {
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
int mini = n, maxi = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = min(mini, arr[i]);
maxi = max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
int main()
{
int A[] = { 4, 5, 1, 3, 2, 6 };
cout << PermuteTheArray(A, 6);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function returns the required count
static int PermuteTheArray(int A[], int n)
{
int []arr = new int[n];
// Store the indices of the
// elements present in A[].
for(int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
int mini = n, maxi = 0;
int count = 0;
for(int i = 0; i < n; i++)
{
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 4, 5, 1, 3, 2, 6 };
System.out.print(PermuteTheArray(A, 6));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to implement
# the above approach
# Function returns the required count
def PermuteTheArray(A, n):
arr = [0] * n
# Store the indices of the
# elements present in A[].
for i in range(n):
arr[A[i] - 1] = i
# Store the maximum and
# minimum index of the
# elements from 1 to i.
mini = n
maxi = 0
count = 0
for i in range(n):
# Update maxi and mini, to
# store minimum and maximum
# index for permutation
# of elements from 1 to i+1
mini = min(mini, arr[i])
maxi = max(maxi, arr[i])
# If difference between maxi
# and mini is equal to i
if (maxi - mini == i):
# Increase count
count += 1
# Return final count
return count
# Driver Code
if __name__ == "__main__":
A = [ 4, 5, 1, 3, 2, 6 ]
print(PermuteTheArray(A, 6))
# This code is contributed by chitranayal
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function returns the required count
static int PermuteTheArray(int []A, int n)
{
int []arr = new int[n];
// Store the indices of the
// elements present in []A.
for(int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
int mini = n, maxi = 0;
int count = 0;
for(int i = 0; i < n; i++)
{
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = Math.Min(mini, arr[i]);
maxi = Math.Max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
public static void Main(String[] args)
{
int []A = { 4, 5, 1, 3, 2, 6 };
Console.Write(PermuteTheArray(A, 6));
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。