Sunday, October 15, 2023
HomeSoftware DevelopmentMaximizing sum of non-consecutive components in Matrix

Maximizing sum of non-consecutive components in Matrix


#embrace <bits/stdc++.h>

utilizing namespace std;

  

int maximumPointsUtil(int index, int dp[],

                      vector<vector<int> >& factors)

{

    if (dp[index] != -1)

        return dp[index];

    int row = index / 3;

    int col = index % 3;

    if (col == 0) {

        dp[index]

            = factors[row][col]

              + max(

                    maximumPointsUtil(index + 4, dp, factors),

                    maximumPointsUtil(index + 5, dp, factors));

        return dp[index];

    }

    else if (col == 1) {

        dp[index]

            = factors[row][col]

              + max(

                    maximumPointsUtil(index + 2, dp, factors),

                    maximumPointsUtil(index + 4, dp, factors));

        return dp[index];

    }

    onsecutive Components in Matrix else

    {

        dp[index]

            = factors[row][col]

              + max(

                    maximumPointsUtil(index + 1, dp, factors),

                    maximumPointsUtil(index + 2, dp, factors));

        return dp[index];

    }

}

int maximumPoints(vector<vector<int> >& factors, int n)

{

    int dp[n * 3];

    memset(dp, -1, sizeof(dp));

    dp[n * 3 - 1] = factors[n - 1][2];

    dp[n * 3 - 2] = factors[n - 1][1];

    dp[n * 3 - 3] = factors[n - 1][0];

    int first = maximumPointsUtil(0, dp, factors);

    int second = maximumPointsUtil(1, dp, factors);

    int third = maximumPointsUtil(2, dp, factors);

    return max(first, max(second, third));

}

  

int predominant()

{

    vector<vector<int> > factors

        = { { 1, 2, 5 }, { 3, 1, 1 }, { 3, 3, 3 } };

    int n = factors.dimension();

  

    

    cout << maximumPoints(factors, n);

    return 0;

}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments