||DP|| 3 Approaches || O(1) space 
IntuitionHere we are painting both ith house and n-i-1th house at a time .
all the three codes from top - down  -- > Tabulation --> space optimised solution is
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Here we are painting both ith house and n-i-1th house at a time .
all the three codes from top - down  -- > Tabulation --> space optimised solution is discussed here.

# Approach
<!-- Describe your approach to solving the problem. -->
while painting ith house we see the previous house paint denoted as prevpaint and while painting n-i-1th house we see n-ith paint denoted as forwpaint. 
the code is self explainatory 

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

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```cpp []
class Solution {
public:

long long func(int i,vector<vector<int>>&cost,int prevpaint,int forwpaint,vector<vector<vector<long long>>>&dp){
    int n = cost.size();
    if(i == n/2){
        return 0;
    }
    if(dp[i][prevpaint][forwpaint] != -1) return dp[i][prevpaint][forwpaint];
    long long mini = 1e17;
      long long temp = 1e17;
    for (int paint1 = 1; paint1 <= 3; paint1++) {
        if(paint1 != prevpaint){
            for(int paint2 = 1;paint2 <=3;paint2++){
                if (paint2 != forwpaint && paint1 != paint2) {
                  temp  = ((cost[i][paint1-1]+cost[n-i-1][paint2-1] )+func(i + 1, cost, paint1,paint2,dp)); 
                    mini = min(mini, temp);
                }
            }
        }
    }
    return dp[i][prevpaint][forwpaint] = mini;
}
    long long minCost(int n, vector<vector<int>>& cost) {
       // top - down 
        // vector<vector<vector<long long>>>dp(n/2+1,vector<vector<long long>>(4,vector<long long>(4,-1)));
        // return func(0,cost,0,0,dp);


        /* tabulation */

    //     vector<vector<vector<long long>>>dp(n/2+1,vector<vector<long long>>(4,vector<long long>(4,0)));
    //     for(int i=n/2-1;i>=0;i--){
    //         for(int prevpaint = 0;prevpaint<=3;prevpaint++){
    //             for(int forwpaint = 0;forwpaint <= 3 ;forwpaint++){
    //          long long mini = 1e17;
    //         long long temp = 1e17;
    //         for (int paint1 = 1; paint1 <= 3; paint1++) {
    //             if(paint1 != prevpaint){
    //                 for(int paint2 = 1;paint2<=3;paint2++){
    //                     if (paint2 != forwpaint && paint1 != paint2) {
    //                     temp  = ((cost[i][paint1-1]+cost[n-i-1][paint2-1] )+dp[i+1][paint1][paint2]); 
    //                         mini = min(mini, temp);
    //                     }
    //                 }
    //             }
    //         }
    //          dp[i][prevpaint][forwpaint] = mini;
    //             }
    //     }
    // }
    //  return dp[0][0][0];

     /* space optimised */

     vector<vector<long long>>cur(4,vector<long long>(4,0));
     vector<vector<long long>>forw(4,vector<long long>(4,0));
    
        for(int i=n/2-1;i>=0;i--){
            for(int prevpaint = 0;prevpaint<=3;prevpaint++){
                for(int forwpaint = 0;forwpaint <= 3 ;forwpaint++){
             long long mini = 1e17;
            long long temp = 1e17;
            for (int paint1 = 1; paint1 <= 3; paint1++) {
                if(paint1 != prevpaint){
                    for(int paint2 = 1;paint2<=3;paint2++){
                        if (paint2 != forwpaint && paint1 != paint2) {
                        temp  = ((cost[i][paint1-1]+cost[n-i-1][paint2-1] )+forw[paint1][paint2]); 
                            mini = min(mini, temp);
                        }
                    }
                }
            }
             cur[prevpaint][forwpaint] = mini;
                }
        }
        forw = cur;
    }
        return forw[0][0];
       
    }
};
```
['C++']