LeetCode 3405: Count the Number of Arrays with K Matching Adjacent Elements Solution

Master LeetCode problem 3405 (Count the Number of Arrays with K Matching Adjacent Elements), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3405. Count the Number of Arrays with K Matching Adjacent Elements

Problem Explanation

Explanation

To solve this problem, we can utilize dynamic programming to count the number of good arrays. We can define a 3D DP array where dp[i][j][k] represents the number of good arrays of length i ending with the element j and having k matching adjacent elements. We can then transition between states based on the conditions given in the problem.

Algorithmic Idea

  1. Initialize a 3D DP array with dimensions (n+1) x (m+1) x (k+1).
  2. Initialize the base cases where dp[1][j][0] = 1 for all j from 1 to m.
  3. Iterate through the DP array, updating the counts based on the conditions provided in the problem.
  4. The total number of good arrays will be the sum of dp[n][j][k] for all j from 1 to m.
  5. Return the result modulo 10^9 + 7.

Time Complexity

The time complexity of this approach is O(n * m * k) where n is the length of the array, m is the range of elements, and k is the number of matching adjacent elements.

Space Complexity

The space complexity of this approach is O(n * m * k) for the DP array.

Solution Code

class Solution {
    public int numOfArrays(int n, int m, int k) {
        int mod = 1000000007;
        int[][][] dp = new int[n + 1][m + 1][k + 1];
        
        for (int j = 1; j <= m; j++) {
            dp[1][j][0] = 1;
        }
        
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= m; j++) {
                for (int kk = 0; kk <= k; kk++) {
                    for (int jj = 1; jj <= m; jj++) {
                        if (j == jj) {
                            dp[i + 1][j][kk + 1] = (dp[i + 1][j][kk + 1] + dp[i][j][kk]) % mod;
                        } else {
                            dp[i + 1][jj][kk] = (dp[i + 1][jj][kk] + dp[i][j][kk]) % mod;
                        }
                    }
                }
            }
        }
        
        int res = 0;
        for (int j = 1; j <= m; j++) {
            res = (res + dp[n][j][k]) % mod;
        }
        
        return res;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3405 (Count the Number of Arrays with K Matching Adjacent Elements)?

This page provides optimized solutions for LeetCode problem 3405 (Count the Number of Arrays with K Matching Adjacent Elements) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3405 (Count the Number of Arrays with K Matching Adjacent Elements)?

The time complexity for LeetCode 3405 (Count the Number of Arrays with K Matching Adjacent Elements) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3405 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3405 in Java, C++, or Python.

Back to LeetCode Solutions