📌  相关文章
📜  计算通过将第一个元素和最后一个元素相同的子数组的所有元素替换为第一个元素任意次数而获得的不同序列

📅  最后修改于: 2021-09-17 06:53:29             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到对给定数组arr[]执行以下操作任意次数后可以形成的不同序列的数量。

例子:

方法:这个问题可以用动态规划解决。请按照以下步骤解决问题:

  • 初始化一个辅助数组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 现场工作专业课程学生竞争性编程现场课程