Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

Matrix chain multiplication 🌟🌟

Tabulation(bottom-up)

int matrixMultiplication(vector<int>& arr, int N)
{
    vector<vector<int>> dp(N, vector<int>(N, 0));
    for (int i = N - 1; i >= 1; i--) {
        for (int j = i + 1; j < N; j++) {
            int mn = INT_MAX;
            for (int k = i; k <= j - 1; k++) {
                int steps = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j];
                mn = min(mn, steps);
            }
            dp[i][j] = mn;
        }
    }
    return dp[1][N - 1];
}