Easy and Excellent code and Recursive Approach


#1

int dp[100000][3];
int sol(int color,int index, vector<vector>&A)
{ if(index==A.size())return 0;
if(index>A.size())return INT_MAX-1000;
if(dp[index][color]!=-1)return dp[index][color];
int result=INT_MAX;
result=min(result,min(A[index][(color)%3]+sol((color+1)%3,index+1,A),A[index][(color)%3]+sol((color+2)%3,index+1,A)));
return dp[index][color]=result;
}
int Solution::solve(vector<vector > &A) {
int n=A.size();
memset(dp,-1,sizeof(dp));
return min(sol(0,0,A),min(sol(1,0,A),sol(2,0,A)));
}