My code fails some test cases. Could someone please point out the flaw or provide test cases which fail…
My approach uses memorization…
int minCost(int cost[] , int N , int i , bool first , int memo[][2])
{
bool prev = first;
if(i == N-1)
{
if(prev == false)
return cost[i];
if(first == true)
return 0;
if(first == false)
return cost[i];
}
if(prev)
{
if(memo[i][0])
return memo[i][0];
else
memo[i][0] = min ( cost[i] + minCost(cost,N,i+1,prev,memo) ,
minCost(cost,N,i+1,!prev,memo));
return memo[i][0];
}
else
{
if(memo[i][1])
return memo[i][1];
else
memo[i][1] = cost[i] + minCost(cost,N,i+1,!prev,memo);
return memo[i][1];
}
}
int main()
{
int N;
cin>>N;
int cost[N];
for(int i=0 ; i<N ; i++)
cin>>cost[i];
int memo[N][2];
for(int i=0 ; i<N ; i++)
memo[i][0] = memo[i][1] = 0;
cout<<min( minCost(cost,N,0,false,memo) ,
minCost(cost,N,0,true,memo));
}