Find maximum possible value of advertising

• Difficulty Level : Hard
• Last Updated : 14 May, 2021

Examples:

Input: Arr[][] = {{0, 10}, {4, 10}, {5, 30}}
N = 3
K = 4
M = 6
Output: 40
Either we can take advertisement starting at 0 and 4 or at 0 and 5.
Maximum Value if 40 if we take 0 and 5 pair.

Input: Arr[][] = {{0, 10}, {4, 110}, {5, 30}}
N = 3
K = 4
M = 6
Output: 120

Approach:

• We will use Dynamic Programming, maintain a dp[M] where
• dp[i] represents the maximum advertising value attained if there is no advertisement starting at i-th minute,
• dp[i] represents the maximum advertising value attained if we select an advertisement starting at ith minute. Final answer will be maximum of dp[M-1] and dp[M-1].
• To build dp states-
• For dp[i], since we no advertisement starts at i-th minute, so there is no restriction of K minutes thus, dp[i] = max(dp[i-1], dp[i-1]) as previous minute we can have both scenario possible.
• For dp[i], now we have advertisement starting at i-th minute, it implies that at minutes i – 1, i – 2, …, i – (K – 1) we can’t have any advertisements. So, we must look at (i – K)-th minute. Thus, dp[i] = value[i] + max(dp[i-K], dp[i-K]), as at minute i – K we can again have both the scenarios.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; #define ll long long int // Function to find maximum// possible advertising valueint max_value(int array[], int M,              int K, int N){    // To store advertising    // value at i-th minute    int time[M] = { 0 };     for (int i = 0; i < N; i++) {        time[array[i]] = array[i];    }     int dp[M];     // Base Case    dp = 0;    dp = time;     for (int i = 1; i < M; i++) {         // If no advertisement is        // taken on ith minute        dp[i] = max(dp[i - 1], dp[i - 1]);         // If advertisement is taken        // on i-th minute        dp[i] = time[i];         if (i - K >= 0) {            dp[i]                += max(dp[i - K], dp[i - K]);        }    }     return max(dp[M - 1], dp[M - 1]);} // Driver's Codeint main(){    // array[] start time    // array[] advertising value    int array[] = {        { 0, 10 },        { 4, 110 },        { 5, 30 }    };     int N = 3;    int K = 4;    int M = 6;     cout << max_value(array, M, K, N);}

Java

 // Java program for the above approachimport java.util.*;import java.lang.*; class GFG{     // Function to find maximum// possible advertising valuestatic int max_value(int array[][], int M,                     int K, int N){         // To store advertising    // value at i-th minute    int[] time = new int[M];       for(int i = 0; i < N; i++)    {        time[array[i]] = array[i];    }       int[][] dp = new int[M];         // Base Case    dp = 0;    dp = time;       for(int i = 1; i < M; i++)    {                 // If no advertisement is        // taken on ith minute        dp[i] = Math.max(dp[i - 1],                            dp[i - 1]);           // If advertisement is taken        // on i-th minute        dp[i] = time[i];           if (i - K >= 0)        {            dp[i] += Math.max(dp[i - K],                                 dp[i - K]);        }    }    return Math.max(dp[M - 1], dp[M - 1]);}   // Driver codepublic static void main(String[] args){         // array[] start time    // array[] advertising value    int array[][] = { { 0, 10 },                      { 4, 110 },                      { 5, 30 } };         int N = 3;    int K = 4;    int M = 6;         System.out.println(max_value(array, M, K, N));}} // This code is contributed by offbeat

Javascript


Output:
120

My Personal Notes arrow_drop_up