给定一个由N 个整数组成的数组arr[] ,任务是找到对给定数组arr[]执行以下操作任意次数后可以形成的不同序列的数量。
Choose two indices i and j such that arr[i] is equal to arr[j] and update all the elements in the range [i, j] in the array to arr[i].
例子:
Input: arr[] = {1, 2, 1, 2, 2}
Output: 3
Explanation:
There can be three possible sequences:
- The initial array {1, 2, 1, 2, 2}.
- Choose indices 0 and 2 and as arr[0](= 1) and arr[2](= 1) are equal and update the array elements arr[] over the range [0, 2] to arr[0](= 1). The new sequence obtained is {1, 1, 1, 2, 2}.
- Choose indices 1 and 3 and as arr[1](= 2) and arr[3](= 2) are equal and update the array elements arr[] over the range [1, 3] to arr[1](= 2). The new sequence obtained is {1, 2, 2, 2, 2}.
Therefore, the total number of sequences formed is 3.
Input: arr[] = {4, 2, 5, 4, 2, 4}
Output: 5
方法:这个问题可以用动态规划解决。请按照以下步骤解决问题:
- 初始化一个辅助数组dp[] ,其中dp[i]存储给定数组arr[]的前i 个元素可能的不同序列的数量,并将dp[0]初始化为1 。
- 初始化一个数组lastOccur[] ,其中lastOccur[i]存储数组的前i 个元素中最后一次出现的元素arr[i] arr[]并用-1初始化lastOccur[0] 。
- 使用变量i迭代范围[1, N]并执行以下步骤:
- 将dp[i]的值更新为dp[i – 1] 。
- 如果当前元素的最后一次出现不等于-1且小于(i – 1) ,则将dp[lastOccur[curEle]]的值添加到dp[i] 。
- 将lastOccur[curEle]的值更新为i 。
- 完成以上步骤后,打印dp[N]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count number of sequences
// satisfying the given criteria
void countPossiblities(int arr[], int n)
{
// Stores the index of the last
// occurrence of the element
int lastOccur[100000];
for (int i = 0; i < n; i++) {
lastOccur[i] = -1;
}
// Initialize an array to store the
// number of different sequences
// that are possible of length i
int dp[n + 1];
// Base Case
dp[0] = 1;
for (int i = 1; i <= n; i++) {
int curEle = arr[i - 1];
// If no operation is applied
// on ith element
dp[i] = dp[i - 1];
// If operation is applied on
// ith element
if (lastOccur[curEle] != -1
& lastOccur[curEle] < i - 1) {
dp[i] += dp[lastOccur[curEle]];
}
// Update the last occurrence
// of curEle
lastOccur[curEle] = i;
}
// Finally, print the answer
cout << dp[n] << endl;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 1, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
countPossiblities(arr, N);
return 0;
}
Java
// Java Program for the above approach
import java.io.*;
class GFG {
// Function to count number of sequences
// satisfying the given criteria
static void countPossiblities(int arr[], int n)
{
// Stores the index of the last
// occurrence of the element
int[] lastOccur = new int[100000];
for (int i = 0; i < n; i++) {
lastOccur[i] = -1;
}
// Initialize an array to store the
// number of different sequences
// that are possible of length i
int[] dp = new int[n + 1];
// Base Case
dp[0] = 1;
for (int i = 1; i <= n; i++) {
int curEle = arr[i - 1];
// If no operation is applied
// on ith element
dp[i] = dp[i - 1];
// If operation is applied on
// ith element
if (lastOccur[curEle] != -1
& lastOccur[curEle] < i - 1) {
dp[i] += dp[lastOccur[curEle]];
}
// Update the last occurrence
// of curEle
lastOccur[curEle] = i;
}
// Finally, print the answer
System.out.println(dp[n]);
}
public static void main(String[] args)
{
int arr[] = { 1, 2, 1, 2, 2 };
int N = arr.length;
countPossiblities(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to count number of sequences
# satisfying the given criteria
def countPossiblities(arr, n):
# Stores the index of the last
# occurrence of the element
lastOccur = [-1] * 100000
# Initialize an array to store the
# number of different sequences
# that are possible of length i
dp = [0] * (n + 1)
# Base Case
dp[0] = 1
for i in range(1, n + 1):
curEle = arr[i - 1]
# If no operation is applied
# on ith element
dp[i] = dp[i - 1]
# If operation is applied on
# ith element
if (lastOccur[curEle] != -1 and
lastOccur[curEle] < i - 1):
dp[i] += dp[lastOccur[curEle]]
# Update the last occurrence
# of curEle
lastOccur[curEle] = i
# Finally, prthe answer
print(dp[n])
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 1, 2, 2 ]
N = len(arr)
countPossiblities(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# Program for the above approach
using System;
class GFG {
// Function to count number of sequences
// satisfying the given criteria
static void countPossiblities(int[] arr, int n)
{
// Stores the index of the last
// occurrence of the element
int[] lastOccur = new int[100000];
for (int i = 0; i < n; i++) {
lastOccur[i] = -1;
}
// Initialize an array to store the
// number of different sequences
// that are possible of length i
int[] dp = new int[n + 1];
// Base Case
dp[0] = 1;
for (int i = 1; i <= n; i++) {
int curEle = arr[i - 1];
// If no operation is applied
// on ith element
dp[i] = dp[i - 1];
// If operation is applied on
// ith element
if (lastOccur[curEle] != -1
& lastOccur[curEle] < i - 1) {
dp[i] += dp[lastOccur[curEle]];
}
// Update the last occurrence
// of curEle
lastOccur[curEle] = i;
}
// Finally, print the answer
Console.WriteLine(dp[n]);
}
public static void Main()
{
int[] arr = { 1, 2, 1, 2, 2 };
int N = arr.Length;
countPossiblities(arr, N);
}
}
// This code is contributed by subham348.
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。