Simple C++ solution with modulo operator


#1

int solve_util(int index,vector<vector >&arr,int c,int n,vector<vector >&dp)
{
if(index>n-1)
return 0;

if(dp[index][c]!=-1)
return dp[index][c];

dp[index][c]= arr[index][c]+
min(solve_util(index+1,arr,(c+1)%3,n,dp),solve_util(index+1,arr,(c+2)%3,n,dp));
return dp[index][c];

}
int Solution::solve(vector<vector > &A)
{
int n=A.size();
vector<vector >dp(n,vector(3,-1));
return min(solve_util(0,A,0,n,dp),min(solve_util(0,A,1,n,dp),solve_util(0,A,2,n,dp)));
}