java easy method
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    long dp[][][] ;
    public long minCost(int n, int[][] cost) {
        dp = new long[n][4][4];
        for(long[][] mat : dp){
            for(long[] row : mat) Arrays.fill(row , -1);
        }
        return solve(0 , 3 , 3 , n , cost);
    }
    long solve(int i , int prev1 , int prev2 , int n , int[][] cost){
        if( i >= n/2) return 0 ; 
        if(dp[i][prev1][prev2] != -1) return dp[i][prev1][prev2];

        long result = Long.MAX_VALUE ;

        for(int j = 0 ; j < 3 ; j++){
            if(j != prev1) // this line save further computation 
            //Saving TLE
            for(int k = 0 ; k < 3 ; k++){
                if(j != k && j != prev1 && k != prev2){
                    result = Math.min(result , cost[i][j] + cost[n-1-i][k] 
                    + solve(i+1 , j , k , n , cost));
                }
            }
        }

        return dp[i][prev1][prev2] = result ;
    }



    public long minCostII(int n, int[][] cost) {
        int numPairs = n / 2;
        List<int[]> pairs = new ArrayList<>(numPairs);
        
        for (int p = 0; p < numPairs; p++) {
            pairs.add(cost[p]);
        }

        long INF = (long)1e18;
        long[][] dpPrev = new long[3][3];

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                // System.out.println("OK");
                dpPrev[i][j] = INF;
            }
        }

        int i = 0 ;
        int j = n - 1;
        for (int c1 = 0; c1 < 3; c1++) {
            for (int c2 = 0; c2 < 3; c2++) {
                if (c1 != c2) {
                    long value = (long) cost[i][c1] + (long) cost[j][c2];
                    dpPrev[c1][c2] = value;
                }
            }
        }

        for (int p = 1; p < numPairs; p++) {
            i = p;
            j = n - 1 - p;
            long[][] dpCurr = new long[3][3];

            for (int c1 = 0; c1 < 3; c1++) {
                for (int c2 = 0; c2 < 3; c2++) {
                    dpCurr[c1][c2] = INF;
                }
            }

            for (int prevC1 = 0; prevC1 < 3; prevC1++) {
                for (int prevC2 = 0; prevC2 < 3; prevC2++) {
                    if (dpPrev[prevC1][prevC2] == INF) continue;

                    for (int c1 = 0; c1 < 3; c1++) {
                        for (int c2 = 0; c2 < 3; c2++) {
                            if (c1 != c2 && c1 != prevC1 && c2 != prevC2) {
                                long val = dpPrev[prevC1][prevC2] + (long) cost[i][c1] + (long) cost[j][c2] ;
                                dpCurr[c1][c2] = Math.min(dpCurr[c1][c2], val);
                            }
                        }
                    }
                }
            }

            for (int c1 = 0; c1 < 3; c1++) {
                for (int c2 = 0; c2 < 3; ++c2) {
                    dpPrev[c1][c2] = dpCurr[c1][c2];
                }
            }
        }

        long result = INF;
        for (int c1 = 0; c1 < 3; c1++) {
            for (int c2 = 0; c2 < 3; ++c2) {
                result = Math.min(result, dpPrev[c1][c2]);
            }
        }
        
        return result;
    }
}
```
['Java']