BAR3 - Editorial

PROBLEM LINK :

Contest

Practice

Author: Himanshu Sharma

Tester: Vipul Ahuja

Editorialist: Himanshu Sharma

DIFFICULTY:

Easy-Medium

PREREQUISITES:

Easy DP, Recursion.

PROBLEM:

chefs mom has collection of N chocolates. Each chocolate has different taste value. Mom has some rules, chef can eat atmost 3 chocolates in a row, to eat next chocolates he must skip atleast 1 chocolate.
Chef wants to get maximum possible taste value.
Find out maximum possible taste value chef can get.

QUICK EXPLANATION:

The above problem is standard DP problem.

Let DP : 2d vector for storing intermediate values.

And let V: vector of elements.

DP[1][1]=DP[1][2]=DP[1][3]=DP[1][4]=V[1];

for i in range 2 to N
     DP[i][0]=max(DP[i-1][0],DP[i-1][1],DP[i-1][2],DP[i-1][3]);
     for j in range 1 to 3
           DP[i][j]=DP[i-1][j]+v[i];

result=max(DP[N][1],DP[1][2]);

Complexity of Above solution is O(N) in worst case.

Solution :

Solution from a participated Team : Solution in cpp

Other Approaches are welcome. Please provide if you have one.