Dynamic Programming Series
Dynamic Programming Series
Day 6
Vacation
(AtCoder Problem)
Problem Description
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
There are N days and on ith 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.
Problem Description
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
There are N days and on ith 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.
Problem Description
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
There are N days and on ith 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.
Problem Description
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.
There are N days and on ith 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.
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
DP[0]
Let's say we have n = 3
6 7 8
8 8 3
2 5 2
| 6 | 7 | 8 |
|---|
8 + max(dp[0][1], dp[0][2])
| 6 | 7 | 8 |
|---|---|---|
| 16 | ||
DP
DP[1]
DP[0]
Let's say we have n = 3
6 7 8
8 8 3
2 5 2
8 + max(dp[0][0], dp[0][2])
| 6 | 7 | 8 |
|---|
| 6 | 7 | 8 |
|---|---|---|
| 16 | 16 | |
DP
DP[1]
DP[0]
Let's say we have n = 3
6 7 8
8 8 3
2 5 2
3 + max(dp[0][0], dp[0][1])
| 6 | 7 | 8 |
|---|
| 6 | 7 | 8 |
|---|---|---|
| 16 | 16 | 10 |
DP
DP[1]
DP[0]
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
2 + max(dp[1][1], dp[1][2])
| 6 | 7 | 8 |
|---|
| 6 | 7 | 8 |
|---|---|---|
| 16 | 16 | 10 |
| 18 |
DP
DP[2]
DP[1]
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
5 + max(dp[1][0], dp[1][2])
| 6 | 7 | 8 |
|---|
| 6 | 7 | 8 |
|---|---|---|
| 16 | 16 | 10 |
| 18 | 21 |
DP
DP[2]
DP[1]
Let's say we have n = 7
6 7 8
8 8 3
2 5 2
2 + max(dp[1][0], dp[1][1])
| 6 | 7 | 8 |
|---|
| 6 | 7 | 8 |
|---|---|---|
| 16 | 16 | 10 |
| 18 | 21 | 18 |
DP
DP[2]
DP[1]
Final answer would be the max of our last DP row.
| 6 | 7 | 8 |
|---|---|---|
| 16 | 16 | 10 |
| 18 | 21 | 18 |
DP
DP[2]
DP[1]
Final Answer = 21
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[n][3];
dp[0][0] = arr[0][0];
dp[0][1] = arr[0][1];
dp[0][2] = arr[0][2];
for (int i = 1;i < n;i++)
{
dp[i][0] = arr[i][0] + max(dp[i-1][1], dp[i-1][2]);
dp[i][1] = arr[i][1] + max(dp[i-1][0], dp[i-1][2]);
dp[i][2] = arr[i][2] + max(dp[i-1][0], dp[i-1][1]);
}
return max(dp[n-1][0], max(dp[n-1][1], dp[n-1][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.
Day 6 vacation problem
By gauravsahu
Day 6 vacation problem
- 59