×

# BAR3 - Editorial

Author: Himanshu Sharma
Tester: Vipul Ahuja
Editorialist: Himanshu Sharma

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

This question is marked "community wiki".

843
accept rate: 7%

 0 Other Approaches are welcome. Please provide if you have one. answered 14 Feb '18, 23:13 84●3 accept rate: 7%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,172
×348

question asked: 14 Feb '18, 23:09

question was seen: 213 times

last updated: 14 Feb '18, 23:13