My Solution C++. Dp memoization
IntuitionReccursively find all the combinations which satisfies both conditions.
As we need to check for equidistant, use two var start and end and check for th
# Intuition
Reccursively find all the combinations which satisfies both conditions.
As we need to check for equidistant, use two var start and end and check for the possible combination.

Use a dp[start][end][startColor][endColor] for memoziation.
Since we are checking for equidistant i.e start==end we can use dp[start][startColor][endColor]

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
$$O(n*4)$$

- Space complexity:
$$O(n*4)$$

# Code
```cpp []
class Solution {

    vector<vector<vector<long long int>>> dp;

    long long int solve(vector<vector<int>>& a,int i,int j,int leftColor,int rightColor){
        if(i>=j) return 0;

        if(dp[i][leftColor][rightColor] != -1) return dp[i][leftColor][rightColor];

        long long int ans = LONG_MAX;
        for(int k = 1;k<=3;k++){
            for(int l=1;l<=3;l++){
                if(k==l || k==leftColor || l==rightColor) continue;
                ans = min(ans,a[i][k-1] + a[j][l-1] + solve(a,i+1,j-1,k,l));
            }
        }
        return dp[i][leftColor][rightColor]=ans;
    }

public:
    long long minCost(int n, vector<vector<int>>& cost) {
        dp = vector<vector<vector<long long int>>>(n,vector<vector<long long int>>(4,vector<long long int>(4,-1)));
        return solve(cost,0,n-1,0,0);
    }
};
```
['C++']