 # PROBLEM LINK: Eat Code Sleep Repeat

Fractal Code Fiesta

EASY-MEDIUM

# PREREQUISITES:

Greedy, DP, Recursion

# PROBLEM:

Given D days , you have to choose one of the three activities on each day such that total Points you can make after D days is maximum. The only restriction is that we cannot do the Same activity on two consecutive days. For example, if we did activity A yesterday, we cannot do activity A today but we can do it tomorrow.

# QUICK EXPLANATION:

Think of a way to consider all the possible cases and then try to optimize it.

# EXPLANATION:

Subtask #1:

Since the constraints in this subtask are very low we can easily implement Brute Force solution using recursion to solve this problem.

We will start by doing one of the three activities on 1st day , then for the next day we have two options before us. For ex. if we did Activity A today, tomorrow we can do either Activity B or Activity C. Hence, we can use recursion to consider all the possible options. Our Answer in the end will be maximum of three values:

• Maximum points we gained by choosing Activity A on first day, P_A
• Maximum points we gained by choosing Activity B on first day, P_B
• Maximum points we gained by choosing Activity C on first day, P_C

Ans = max(P_A, P_B, P_C).

Time Complexity of this approach → T (3*2^D ).

In our recursive function we will have two recursive calls which adds the factor of 2D and since we call this function 3 times so total complexity is T (3*2^D ), which is more than enough to pass this subtask.

Subtask #2:

To solve this version of subtask we will have to use Dynamic programming.

we will maintain a 2D array, dp[n].

Where dp[i][j] = maximum points gained by performing jth activity on ith day.

dp[i] will store the maximum points we can gain by doing activity A on day i.
dp[i] will store the maximum points we can gain by doing activity B on day i.
dp[i] will store the maximum points we can gain by doing activity C on day i.

Our base cases will be:

// If you watch netflix on the first day, you cannot get more than x points.

dp = x;

// If you play counter strike on the first day, you cannot get more than y points.

dp = y;

// If you sleep on the first day, you cannot get more than z points.

dp = z;

For every day i, as we cannot do the activity we did on the previous day,

// If we do activity A today, we have to have done activities B or C on the previous day.

dp[i] = x[i] + max(dp[i - 1], dp[i - 1]);

// If we do activity B today, we have to have done activities A or C on the previous day.

dp[i] = y[i] + max(dp[i - 1], dp[i - 1]);

// If we do activity C today, we have to have done activities A or B on the previous day.

dp[i] = z[i] + max(dp[i - 1], dp[i - 1]);

Simply by checking for each day i, we can calculate the best we can do by the end of our last day.
As we can end by doing activities A, B, or C, our answer will be the maximum points gained on doing activities A, B, or C on the last day.
Time complexity: O(n)

# SOLUTIONS:

For Subtask #1:

Editorialist's Solution
``````#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mxn = 20;
ll activity[mxn], D;
ll cal(ll day, ll act)
{
if (day > D)
return 0;

if (act == 0)
return activity[day][act] + max(cal(day + 1, 1), cal(day + 1, 2));

else if (act == 1)
return activity[day][act] + max(cal(day + 1, 0), cal(day + 1, 2));

else
return activity[day][act] + max(cal(day + 1, 0), cal(day + 1, 1));
}
int main()
{
ll t = 1;
while (t--)
{
ll i, j, k, n, m;
cin >> D;
for (i = 1; i <= D; i++)
{
cin >> activity[i] >> activity[i] >> activity[i];
}
ll ans = max({cal(1, 0), cal(1, 1), cal(1, 2)});
cout << ans;
}
return 0;
}
``````

For Subtask #2:

Editorialist's Solution
``````//Using Memoization Recursion
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mxn = 1e5 + 5;
ll activity[mxn], D;
ll dp[mxn];
ll cal(ll day, ll act)
{
if (day > D)
return 0;

if (dp[day][act] != -1)
return dp[day][act];

if (act == 0)
return dp[day][act] = activity[day][act] + max(cal(day + 1, 1), cal(day + 1, 2));

else if (act == 1)
return dp[day][act] = activity[day][act] + max(cal(day + 1, 0), cal(day + 1, 2));

else
return dp[day][act] = activity[day][act] + max(cal(day + 1, 0), cal(day + 1, 1));
}
int main()
{
ll t = 1;
while (t--)
{
ll i, j, k, n, m;
cin >> D;
memset(dp, -1, sizeof(dp));
for (i = 1; i <= D; i++)
{
cin >> activity[i] >> activity[i] >> activity[i];
}
ll ans = max({cal(1, 0), cal(1, 1), cal(1, 2)});
cout << ans;
}
return 0;
}
``````
Editorialist's Solution
``````//DP Using Iterative Approach
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mxn = 1e5 + 5;
ll activity[mxn], D;
ll dp[mxn];
int main()
{
ll t = 1;
while (t--)
{
ll i, j, k, n, m;
cin >> D;
for (i = 1; i <= D; i++)
{
cin >> activity[i] >> activity[i] >> activity[i];
}
dp = activity;
dp = activity;
dp = activity;

for (i = 2; i <= D; i++)
{
dp[i] = activity[i] + max(dp[i - 1], dp[i - 1]);
dp[i] = activity[i] + max(dp[i - 1], dp[i - 1]);
dp[i] = activity[i] + max(dp[i - 1], dp[i - 1]);
}
ll ans = max({dp[D], dp[D], dp[D]});
cout << ans;
}
return 0;
}
``````