Highway Billboard Problem
Last Updated :
05 Aug, 2021
Consider a highway of M miles. The task is to place billboards on the highway such that revenue is maximized. The possible sites for billboards are given by number x1 < x2 < ….. < xn-1 < xn, specifying positions in miles measured from one end of the road. If we place a billboard at position xi, we receive a revenue of ri > 0. There is a restriction that no two billboards can be placed within t miles or less than it.
Note : All possible sites from x1 to xn are in range from 0 to M as need to place billboards on a highway of M miles.
Examples:
Input : M = 20
x[] = {6, 7, 12, 13, 14}
revenue[] = {5, 6, 5, 3, 1}
t = 5
Output: 10
By placing two billboards at 6 miles and 12
miles will produce the maximum revenue of 10.
Input : M = 15
x[] = {6, 9, 12, 14}
revenue[] = {5, 6, 3, 7}
t = 2
Output : 18
Let maxRev[i], 1 <= i <= M, be the maximum revenue generated from beginning to i miles on the highway. Now for each mile on the highway, we need to check whether this mile has the option for any billboard, if not then the maximum revenue generated till that mile would be same as maximum revenue generated till one mile before. But if that mile has the option for billboard then we have 2 options:
1. Either we will place the billboard, ignore the billboard in previous t miles, and add the revenue of the billboard placed.
2. Ignore this billboard. So maxRev[i] = max(maxRev[i-t-1] + revenue[i], maxRev[i-1])
Below is implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxRevenue( int m, int x[], int revenue[], int n,
int t)
{
int maxRev[m+1];
memset (maxRev, 0, sizeof (maxRev));
int nxtbb = 0;
for ( int i = 1; i <= m; i++)
{
if (nxtbb < n)
{
if (x[nxtbb] != i)
maxRev[i] = maxRev[i-1];
else
{
if (i <= t)
maxRev[i] = max(maxRev[i-1], revenue[nxtbb]);
else
maxRev[i] = max(maxRev[i-t-1]+revenue[nxtbb],
maxRev[i-1]);
nxtbb++;
}
}
else
maxRev[i] = maxRev[i - 1];
}
return maxRev[m];
}
int main()
{
int m = 20;
int x[] = {6, 7, 12, 13, 14};
int revenue[] = {5, 6, 5, 3, 1};
int n = sizeof (x)/ sizeof (x[0]);
int t = 5;
cout << maxRevenue(m, x, revenue, n, t) << endl;
return 0;
}
|
Java
class GFG
{
static int maxRevenue( int m, int [] x,
int [] revenue,
int n, int t)
{
int [] maxRev = new int [m + 1 ];
for ( int i = 0 ; i < m + 1 ; i++)
maxRev[i] = 0 ;
int nxtbb = 0 ;
for ( int i = 1 ; i <= m; i++)
{
if (nxtbb < n)
{
if (x[nxtbb] != i)
maxRev[i] = maxRev[i - 1 ];
else
{
if (i <= t)
maxRev[i] = Math.max(maxRev[i - 1 ],
revenue[nxtbb]);
else
maxRev[i] = Math.max(maxRev[i - t - 1 ] +
revenue[nxtbb],
maxRev[i - 1 ]);
nxtbb++;
}
}
else
maxRev[i] = maxRev[i - 1 ];
}
return maxRev[m];
}
public static void main(String []args)
{
int m = 20 ;
int [] x = new int []{ 6 , 7 , 12 , 13 , 14 };
int [] revenue = new int []{ 5 , 6 , 5 , 3 , 1 };
int n = x.length;
int t = 5 ;
System.out.println(maxRevenue(m, x, revenue, n, t));
}
}
|
Python3
def maxRevenue(m, x, revenue, n, t) :
maxRev = [ 0 ] * (m + 1 )
nxtbb = 0 ;
for i in range ( 1 , m + 1 ) :
if (nxtbb < n) :
if (x[nxtbb] ! = i) :
maxRev[i] = maxRev[i - 1 ]
else :
if (i < = t) :
maxRev[i] = max (maxRev[i - 1 ],
revenue[nxtbb])
else :
maxRev[i] = max (maxRev[i - t - 1 ] +
revenue[nxtbb],
maxRev[i - 1 ]);
nxtbb + = 1
else :
maxRev[i] = maxRev[i - 1 ]
return maxRev[m]
if __name__ = = "__main__" :
m = 20
x = [ 6 , 7 , 12 , 13 , 14 ]
revenue = [ 5 , 6 , 5 , 3 , 1 ]
n = len (x)
t = 5
print (maxRevenue(m, x, revenue, n, t))
|
C#
using System;
class GFG
{
static int maxRevenue( int m, int [] x,
int [] revenue,
int n, int t)
{
int [] maxRev = new int [m + 1];
for ( int i = 0; i < m + 1; i++)
maxRev[i] = 0;
int nxtbb = 0;
for ( int i = 1; i <= m; i++)
{
if (nxtbb < n)
{
if (x[nxtbb] != i)
maxRev[i] = maxRev[i - 1];
else
{
if (i <= t)
maxRev[i] = Math.Max(maxRev[i - 1],
revenue[nxtbb]);
else
maxRev[i] = Math.Max(maxRev[i - t - 1] +
revenue[nxtbb],
maxRev[i - 1]);
nxtbb++;
}
}
else
maxRev[i] = maxRev[i - 1];
}
return maxRev[m];
}
static void Main()
{
int m = 20;
int [] x = new int []{6, 7, 12, 13, 14};
int [] revenue = new int []{5, 6, 5, 3, 1};
int n = x.Length;
int t = 5;
Console.Write(maxRevenue(m, x, revenue, n, t));
}
}
|
PHP
<?php
function maxRevenue( $m , $x ,
$revenue ,
$n , $t )
{
$maxRev = array_fill (0, $m + 1,
false);
$nxtbb = 0;
for ( $i = 1; $i <= $m ; $i ++)
{
if ( $nxtbb < $n )
{
if ( $x [ $nxtbb ] != $i )
$maxRev [ $i ] = $maxRev [ $i - 1];
else
{
if ( $i <= $t )
$maxRev [ $i ] = max( $maxRev [ $i - 1],
$revenue [ $nxtbb ]);
else
$maxRev [ $i ] = max( $maxRev [ $i - $t - 1] +
$revenue [ $nxtbb ],
$maxRev [ $i - 1]);
$nxtbb ++;
}
}
else
$maxRev [ $i ] = $maxRev [ $i - 1];
}
return $maxRev [ $m ];
}
$m = 20;
$x = array (6, 7, 12, 13, 14);
$revenue = array (5, 6, 5, 3, 1);
$n = sizeof( $x );
$t = 5;
echo maxRevenue( $m , $x ,
$revenue , $n , $t );
?>
|
Javascript
<script>
function maxRevenue(m,x,revenue,n,t)
{
let maxRev = new Array(m + 1);
for (let i = 0; i < m + 1; i++)
maxRev[i] = 0;
let nxtbb = 0;
for (let i = 1; i <= m; i++)
{
if (nxtbb < n)
{
if (x[nxtbb] != i)
maxRev[i] = maxRev[i - 1];
else
{
if (i <= t)
maxRev[i] = Math.max(maxRev[i - 1],
revenue[nxtbb]);
else
maxRev[i] = Math.max(maxRev[i - t - 1] +
revenue[nxtbb],
maxRev[i - 1]);
nxtbb++;
}
}
else
maxRev[i] = maxRev[i - 1];
}
return maxRev[m];
}
let m = 20;
let x=[6, 7, 12, 13, 14];
let revenue=[5, 6, 5, 3, 1];
let n = x.length;
let t = 5;
document.write(maxRevenue(m, x, revenue, n, t));
</script>
|
Output:
10
Time Complexity: O(M), where M is distance of total Highway.
Auxiliary Space: O(M).
Source :
https://courses.cs.washington.edu/courses/cse421/06au/slides/Lecture18/Lecture18.pdf
Share your thoughts in the comments
Please Login to comment...