Skip to content
Related Articles

Related Articles

Minimum cost to complete given tasks if cost of 1, 7 and 30 days are given

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 17 Nov, 2021
View Discussion
Improve Article
Save Article

Given a sorted array arr[] consisting of N positive integers such that arr[i] represent the days in which a worker will work and an array cost[] of size 3 representing the salary paid to the workers for 1 day, 7 days and 30 days respectively, the task is to find the minimum cost required to have a worker for all the given days in arr[].

Examples:

Input: arr[] = [2, 4, 6, 7, 8, 10, 17], cost[] = [3, 8, 20]
Output: 14
Explanation:
Below is one of the possible ways of hiring workers with minimum cost:

  1. On day 2, call a worker for 1 day which costs cost[0] = 3.
  2. On day 4, call a worker for 7-day which costs cost[1] = 8, which covers day 4, 5, …, 10.
  3. On day 17, call worker for 1-day which costs cost[0] = 3, which covers day 17.

Therefore, the total cost is 3 + 8 + 3 = 14, which is minimum among all possible combinations of hiring workers.

Input: arr[]= [1, 2, 3, 4, 6, 7, 8, 9, 11, 15, 20, 29], cost = [3, 8, 10]
Output: 10

Approach: The given above problem can be solved using Dynamic Programming because it has Optimal Substructure and Overlapping Subproblems. Follow the steps below to solve the problem:

  • Initialize an array, say dp[] where dp[i] stores the minimum cost required to have a worker on the days [i, arr[N – 1]].
  • Initialize the value of dp[arr[N – 1]] as the minimum of {cost[0], cost[1], cost[2]}.
  • Initialize a variable, say ptr that points at the current element of the array arr[].
  • Iterate over the range [arr[N – 1] – 1, 0] using the variable i and perform the following steps:
    1. If the value of ptr >= 0 and arr[ptr] == i then,
      • Initialize a variable, say val1 and modify the value as dp[i + 1] + cost[0].
      • Initialize a variable, say val2 and modify the value as dp[i + 7] + cost[1].
      • Initialize a variable say val3 and modify the value as dp[i + 30] + cost[2].
      • Now, update the value of dp[i] as the minimum of {val1, val2, val3}.
      • Decrease the value of ptr by 1.
    2. Otherwise, update the value of dp[i] as dp[i + 1].
  • After completing the above steps, print the value of dp[1] as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum cost
// to hire the workers for the given
// days in the array days[]
int MinCost(int days[], int cost[], int N)
{
    int size = days[N - 1] + 1;
 
    // Initialize the array dp
    int dp[size];
 
    // Minimum Cost for Nth day
    dp[size - 1] = min(cost[0],
                       min(cost[1],
                           cost[2]));
 
    // Pointer of the array arr[]
    int ptr = N - 2;
 
    // Traverse from right to left
    for (int i = size - 2; i > 0; i--) {
 
        if (ptr >= 0 && days[ptr] == i) {
 
            // If worker is hired for 1 day
            int val1 = dp[i + 1] + cost[0];
 
            // If worker is hired for 7 days
            int val2 = cost[1]
                       + ((i + 7 >= size)
                              ? 0
                              : dp[i + 7]);
 
            // If worker is hired for 30 days
            int val3
                = cost[2]
                  + ((i + 30 >= size)
                         ? 0
                         : dp[i + 30]);
 
            // Update the value of dp[i] as
            // minimum of 3 options
            dp[i] = min(val1, min(val2, val3));
            ptr--;
        }
 
        // If the day is not at the
        // array arr[]
        else {
            dp[i] = dp[i + 1];
        }
    }
 
    // Return the answer
    return dp[1];
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 6, 7, 8, 10, 17 };
    int cost[] = { 3, 8, 20 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << MinCost(arr, cost, N);
 
    return 0;
}


Java




// Java program for the above approach
public class GFG
{
 
// Function to find the minimum cost
// to hire the workers for the given
// days in the array days[]
static int MinCost(int days[], int cost[], int N)
{
    int size = days[N - 1] + 1;
 
    // Initialize the array dp
    int []dp = new int[size];
 
    // Minimum Cost for Nth day
    dp[size - 1] = Math.min(cost[0], Math.min(cost[1], cost[2]));
 
    // Pointer of the array arr[]
    int ptr = N - 2;
 
    // Traverse from right to left
    for (int i = size - 2; i > 0; i--) {
 
        if (ptr >= 0 && days[ptr] == i) {
 
            // If worker is hired for 1 day
            int val1 = dp[i + 1] + cost[0];
 
            // If worker is hired for 7 days
            int val2 = cost[1]  + ((i + 7 >= size)
                              ? 0
                              : dp[i + 7]);
 
            // If worker is hired for 30 days
            int val3
                = cost[2]
                  + ((i + 30 >= size)
                         ? 0
                         : dp[i + 30]);
 
            // Update the value of dp[i] as
            // minimum of 3 options
            dp[i] = Math.min(val1, Math.min(val2, val3));
            ptr--;
        }
 
        // If the day is not at the
        // array arr[]
        else {
            dp[i] = dp[i + 1];
        }
    }
 
    // Return the answer
    return dp[1];
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 2, 4, 6, 7, 8, 10, 17 };
    int cost[] = { 3, 8, 20 };
    int N = arr.length;
    System.out.println(MinCost(arr, cost, N));
}
}
 
// This code is contributed by SoumikMondal


Python3




# Python Program for the above approach
 
# Function to find the minimum cost
# to hire the workers for the given
# days in the array days[]
def MinCost(days, cost, N):
    
    size = days[N - 1] + 1
 
    # Initialize the array dp
    dp = [0 for i in range(size)]
 
    # Minimum Cost for Nth day
    dp[size - 1] = min(cost[0], min(cost[1], cost[2]))
 
    # Pointer of the array arr[]
    ptr = N - 2
 
    # Traverse from right to left
    for i in range(size - 2, 0, -1):
 
        if (ptr >= 0 and days[ptr] == i):
 
            # If worker is hired for 1 day
            val1 = dp[i + 1] + cost[0]
 
            # If worker is hired for 7 days
            val2 = cost[1] + ( 0 if (i + 7 >= size) else dp[i + 7])
 
            # If worker is hired for 30 days
            val3 = cost[2] + ( 0 if (i + 30 >= size) else dp[i + 30])
 
            # Update the value of dp[i] as
            # minimum of 3 options
            dp[i] = min(val1, min(val2, val3))
            ptr -= 1;
 
        # If the day is not at the
        # array arr[]
        else:
            dp[i] = dp[i + 1]
     
    # Return the answer
    return dp[1]
 
# Driver Code
arr = [2, 4, 6, 7, 8, 10, 17]
cost = [3, 8, 20]
N = len(arr)
print(MinCost(arr, cost, N))
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum cost
// to hire the workers for the given
// days in the array days[]
static int MinCost(int[] days, int[] cost, int N)
{
    int size = days[N - 1] + 1;
 
    // Initialize the array dp
    int[] dp = new int[size];
 
    // Minimum Cost for Nth day
    dp[size - 1] = Math.Min(
        cost[0], Math.Min(cost[1], cost[2]));
 
    // Pointer of the array arr[]
    int ptr = N - 2;
 
    // Traverse from right to left
    for(int i = size - 2; i > 0; i--)
    {
        if (ptr >= 0 && days[ptr] == i)
        {
             
            // If worker is hired for 1 day
            int val1 = dp[i + 1] + cost[0];
 
            // If worker is hired for 7 days
            int val2 = cost[1] + ((i + 7 >= size) ?
                            0 : dp[i + 7]);
 
            // If worker is hired for 30 days
            int val3 = cost[2] + ((i + 30 >= size) ?
                            0 : dp[i + 30]);
 
            // Update the value of dp[i] as
            // minimum of 3 options
            dp[i] = Math.Min(val1, Math.Min(val2, val3));
            ptr--;
        }
 
        // If the day is not at the
        // array arr[]
        else
        {
            dp[i] = dp[i + 1];
        }
    }
 
    // Return the answer
    return dp[1];
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 2, 4, 6, 7, 8, 10, 17 };
    int[] cost = { 3, 8, 20 };
    int N = arr.Length;
     
    Console.WriteLine(MinCost(arr, cost, N));
}
}
 
// This code is contributed by subhammahato348


Javascript




<script>
        // JavaScript Program for the above approach
 
        // Function to find the minimum cost
        // to hire the workers for the given
        // days in the array days[]
        function MinCost(days, cost, N)
        {
            let size = days[N - 1] + 1;
 
            // Initialize the array dp
            let dp = new Array(size);
 
            // Minimum Cost for Nth day
            dp[size - 1] = Math.min(cost[0],
                Math.min(cost[1],
                    cost[2]));
 
            // Pointer of the array arr[]
            let ptr = N - 2;
 
            // Traverse from right to left
            for (let i = size - 2; i > 0; i--) {
 
                if (ptr >= 0 && days[ptr] == i) {
 
                    // If worker is hired for 1 day
                    let val1 = dp[i + 1] + cost[0];
 
                    // If worker is hired for 7 days
                    let val2 = cost[1]
                        + ((i + 7 >= size)
                            ? 0
                            : dp[i + 7]);
 
                    // If worker is hired for 30 days
                    let val3
                        = cost[2]
                        + ((i + 30 >= size)
                            ? 0
                            : dp[i + 30]);
 
                    // Update the value of dp[i] as
                    // minimum of 3 options
                    dp[i] = Math.min(val1, Math.min(val2, val3));
                    ptr--;
                }
 
                // If the day is not at the
                // array arr[]
                else {
                    dp[i] = dp[i + 1];
                }
            }
 
            // Return the answer
            return dp[1];
        }
 
        // Driver Code
        let arr = [2, 4, 6, 7, 8, 10, 17];
        let cost = [3, 8, 20];
        let N = arr.length;
        document.write(MinCost(arr, cost, N));
 
    // This code is contributed by Potta Lokesh
 
    </script>


Output: 

14

 

Time Complexity: O(M), where M is the maximum element of the array.
Auxiliary Space: O(M)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!