Dynamic Programming Series
Dynamic Programming Series
Day 6
Vacation
(AtCoder Problem)
Problem Description
There are N days and Each day you can perform any of the three activities:
- A: Swim in the sea. Gain points of happiness.
- B: Catch bugs in the mountains. Gain points of happiness.
- C: Do homework at home. Gain points of happiness
You can not do the same activity for two or more consecutive days.
Find the maximum possible total points of happiness that you can have after n days.
For Example:
for n = 2 days
Day 1: 10 20 30
Day 2: 1 3 100
You can choose a on day 1 and b on day 2
total happiness = 10 + 3 = 13
a
b
c
Problem Description
There are N days and Each day you can perform any of the three activities:
- A: Swim in the sea. Gain points of happiness.
- B: Catch bugs in the mountains. Gain points of happiness.
- C: Do homework at home. Gain points of happiness
You can not do the same activity for two or more consecutive days.
Find the maximum possible total points of happiness that you can have after n days.
For Example:
for n = 2 days
Day 1: 10 20 30
Day 2: 1 3 100
a
b
c
You can choose b on day 1 and c on day 2
total happiness = 20 + 1 = 21
Problem Description
There are N days and Each day you can perform any of the three activities:
- A: Swim in the sea. Gain points of happiness.
- B: Catch bugs in the mountains. Gain points of happiness.
- C: Do homework at home. Gain points of happiness
You can not do the same activity for two or more consecutive days.
Find the maximum possible total points of happiness that you can have after n days.
For Example:
for n = 2 days
Day 1: 10 20 30
Day 2: 1 3 100
a
b
c
similarly, if we keep on doing like this we will get maximum happiness for choosing b on day 1 and c on day 2
Total Happiness = 20 + 100 = 120
Problem Description
There are N days and Each day you can perform any of the three activities:
- A: Swim in the sea. Gain points of happiness.
- B: Catch bugs in the mountains. Gain points of happiness.
- C: Do homework at home. Gain points of happiness
You can not do the same activity for two or more consecutive days.
Find the maximum possible total points of happiness that you can have after n days.
For Example:
for n = 1 days
Day 1: 10 20 30
a
b
c
for only a single day you can perform either a on that day or b on that day or c on that day.
Since you have to maximize happiness therefore you will choose c.
Now Let's Talk about the Problem.
Can we do it with a greedy approach, by selecting maximum happiness available on each day!
Let's see how
take n = 3
3 4 5
1 2 1
4 2 1
Now Let's Talk about the Problem.
Can we do it with a greedy approach, by selecting maximum happiness available on each day!
Let's see how
take n = 3
3 4 5
1 2 1
4 2 1
Now Let's Talk about the Problem.
Can we do it with a greedy approach, by selecting maximum happiness available on each day!
Let's see how
take n = 3
3 4 5
1 2 1
4 2 1
Now Let's Talk about the Problem.
Can we do it with a greedy approach, by selecting maximum happiness available on each day!
Let's see how
Total Happiness = 5 + 2 + 4 = 11
Yes that's a right answer.
take n = 3
3 4 5
1 2 1
4 2 1

Do you really think this approach will work here. Let's take one more example.
Example n = 2
10 20 30
4 5 100
Do you really think this approach will work here. Let's take one more example.
Example n = 2
10 20 30
4 5 100
Do you really think this approach will work here. Let's take one more example.
Example n = 2
10 20 30
4 5 100
Total Happiness = 35
Wrong!!!
It Should be 100 + 20 = 120
So Please Don't be greedy!
Generally if greedy doesn't work we will have to try all possible solution to get optimized result.
Yes, we will do that but with memoization that makes our work easy.
Let's say we have n = 3
6 7 8
8 8 3
2 5 2
Let's say we have n = 3
6 7 8
8 8 3 2 5 2
| 6 | 7 | 8 |
|---|
DP
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
| 6 | 7 | 8 |
|---|
DP
8 + max(dp[1], dp[2])
| 16 |
|---|
DP
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
| 6 | 7 | 8 |
|---|
DP
8 + max(dp[0], dp[2])
| 16 | 16 |
|---|
DP
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
| 6 | 7 | 8 |
|---|
DP
3 + max(dp[0], dp[1])
| 16 | 16 | 10 |
|---|
DP
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
2 + max(dp[1], dp[2])
| 16 | 16 | 10 |
|---|
DP
| 28 |
|---|
DP
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
5 + max(dp[0], dp[2])
| 16 | 16 | 10 |
|---|
DP
| 28 | 26 |
|---|
DP
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
2 + max(dp[0], dp[1])
| 16 | 16 | 10 |
|---|
DP
| 28 | 26 | 27 |
|---|
DP
Final answer would be the max of our dp array.
| 28 | 26 | 27 |
|---|
DP
Let's Understand the code of this approach
int maxHappiness(int n, int arr[][3])
{
//arr will be of size n X 3
int dp[3] = {0};
dp[0] = arr[0][0];
dp[1] = arr[0][1];
dp[2] = arr[0][2];
for (int i = 1;i < n;i++)
{
int t1,t2,t3;
t1 = arr[i][0] + max(dp[1], dp[2]);
t2 = arr[i][1] + max(dp[0], dp[2]);
t3 = arr[i][2] + max(dp[0], dp[1]);
dp[0] = t1;
dp[1] = t2;
dp[2] = t3;
}
return max(dp[0], max(dp[1], dp[2]));
}So that's It for Day 6
So that's It for Day 6
Concepts that we have learnt
So that's It for Day 6
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Greedy Vs Dynamic Programming.
So that's It for Day 6
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Greedy Vs Dynamic Programming.
2. Use of optical Substructure with memoization
So that's It for Day 6
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Greedy Vs Dynamic Programming.
2. Use of optical Substructure with memoization
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.
Copy of Day 6 vacation problem
By Chirayu Jain
Copy of Day 6 vacation problem
- 50