Introduction
Here we will be discussing an interesting problem. We will cover two approaches to solve this problem. First, we will see the native recursive approach and then implement the dynamic programming-based solution using the concept of memoization.
Problem statement
You are given an array consisting of 0,1,2,3 only where the value at ith index represents some specific work that can be done on the ith day.
0 represents no tasks available on that day.
1 represents task type A available for that day.
2 represents task type B available for that day.
3 represents both tasks A and B available for that day. You can perform any one of your choices.
Provided that you can’t perform the same type of task on two consecutive days, you need to find the minimum number of days in which no task is performed.
Input
arr[ ] = { 0, 1, 3, 2 }
Output
minimum days with no task performed = 2
Explanation
On the 0th day, no task is performed as arr[0]=0,i.e count=1. On 1st day, task A can be performed. On the 2nd-day task B can be performed, as pervious day’s task is A. But on the 3rd-day task B can not be performed as the previous day’s task was B. So, no task is performed on the 3rd day. Therefore, count=1+1=2
Note: Please try to solve the problem first and then see the below solution approach.