Welcome back, dear reader. So, in this article, we will discuss a very interesting question called HIGHWAY BILLBOARDS. What does this question say?
Important Links : Problem Link, Solution Video
Welcome back, dear reader. So, in this article, we will discuss a very interesting question called HIGHWAY BILLBOARDS. What does this question say?
Important Links : Problem Link, Solution Video
Have a look at the sample input shown below:
The first input 20 denotes that there is a highway of 20 units length. The next integer 5 denotes the number of billboards that we can place on that road. The next 5 integers denote the positions at which we can place a billboard. For instance, we can place a billboard at the 6th mile or 7th mile and so on. The next 5 numbers show the revenues generated by the corresponding billboards. The last integer 5 denotes the minimum distance between 2 billboards that should be maintained. So, we have to tell the maximum revenue that can be generated. The situation is shown below:
You may refer to the HIGHWAY BILLBOARDS VIDEO to understand the question completely and clear your doubts regarding the question if any. We also suggest you try to solve these problems on your own first and then go to the solution to build your concepts.
Let us take the example given below. In this example, the road is of length 20 units and there are 5 boards that can be kept on the following locations {6,8,12,14,16} and their corresponding revenues are {5,8,5,3,1}. Also, we have to maintain a distance of more than 3 units between 2 consecutive boards.
Let us start with our first approach.
Let us solve this problem following the 3 stages of Dynamic Programming.
Now, let us move to index 2. Again, it is mandatory to select the 2nd board now. So, the revenue will be at least 5 units.
Now, we will check whether we can put any billboards before this current billboard. So, the current billboard is at 12 units location and the revenue for it is 5. The 0th billboard can be selected as it is present at 6 units location and the difference between the current billboard location (12) and the 0th billboard location (6) is 12-6= 6 >3. So, if we put the 0th billboard and then the 2nd billboard, the revenue that can be generated is 5+5=10.
We can also select the 1st billboard in this case because the difference between their locations is 12-8=4>3. So, the revenue in this case will be rev[1] + rev[2]=8+5=13.
So, the maximum revenue that we can generate till this location when the current billboard is mandatory to be selected is 13.
Now, we are at index 3. So, it is mandatory to select this billboard i.e. x[3]=14 units location. Let us now see what the max revenue that can be generated. So, can we select the 0th billboard? Think!!!!
Ye, we can as the distance between them is greater than 3. The total revenue in this case will be 5+3=8.
If we select the 1st billboard, the revenue will be 8+3=11.
We can't select the 3rd billboard as the distance between them is less than 3. So, we have to select a maximum from 11 and 8 and the answer is 11.
So, dear reader, we hope that you have understood the process till here. We recommend you refer to the solution video to understand the concept and clear your doubts regarding it if any. We also request you fill the last element of this dp array yourself. The completely filled array is shown below:
So, the max revenue that we can generate following all the given conditions is 14 and that is when we select the 1st, 2nd and 4th billboard.
Note: It is not necessary that the answer will be dp[dp.length-1]. The answer will be maximum value stores in the dp array. You can easily conclude this by looking at the dp array above as it is not sorted.
Now that we have understood the complete procedure, let us write the code for the same.
java; true";
The above code is written and explained in the solution video (). You may refer to it if you have any doubts about it.
The time complexity of the above solution is O(n2) where n is the number of billboards as we have a nested loop and the inner loop runs for n(n+1)/2 times. Why? Think!!!
The space complexity of the above solution is O(n) as we have created an array of size n where n is the number of billboards.
In the previous method, the complexity of the code was dependent on the number of billboards. In this method, it will depend upon the length of the road. So, we were given a road of length 20. So, let's start the procedure.
java; true";
The above code is written and explained in the solution video (). You may refer to it to clear your doubts. Let us now discuss the time and space complexity of this solution.
The time complexity of the above solution is O(m) where m is the length of the highway because we have just traversed an array of size m+1 once.
The space complexity of the above solution is also O(m) as we have created a dp array of size m+1.
So, dear reader, we hope that you have understood the complete solution. Still, if you have any doubts regarding anything, refer to the complete solution video to clear them. With this, we have completed the article.