给定一个由N 个正整数组成的排序数组arr[],其中arr[i]表示工人将工作的天数,大小为3的数组cost[]表示支付给工人1 天、 7 天和分别为30 天,任务是在arr[] 中找到在所有给定天数内拥有一个工人所需的最低成本。
例子:
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:
- On day 2, call a worker for 1 day which costs cost[0] = 3.
- On day 4, call a worker for 7-day which costs cost[1] = 8, which covers day 4, 5, …, 10.
- 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
方法:上面给出的问题可以使用动态规划解决,因为它具有最优子结构和 oOverlapping 子问题。请按照以下步骤解决问题:
- 初始化一个数组,比如dp[] ,其中dp[i]存储在[i, arr[N – 1]]天拥有一个工人所需的最低成本。
- 将dp[arr[N – 1]]的值初始化为{cost[0], cost[1], cost[2]}的最小值。
- 初始化一个变量,比如ptr指向数组arr[]的当前元素。
- 使用变量i迭代范围[arr[N – 1] – 1, 0]并执行以下步骤:
- 如果ptr的值>= 0并且arr[ptr] == i那么,
- 初始化一个变量,比如val1并将值修改为dp[i + 1] + cost[0] 。
- 初始化一个变量,比如val2并将值修改为dp[i + 7] + cost[1] 。
- 初始化一个变量比如val3并将值修改为dp[i + 30] + cost[2] 。
- 现在,将dp[i]的值更新为{val1, val2, val3}的最小值。
- 将ptr的值减少1 。
- 否则,将dp[i]的值更新为dp[i + 1] 。
- 如果ptr的值>= 0并且arr[ptr] == i那么,
- 完成以上步骤后,打印dp[1]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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]))
# Poleter 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
输出:
14
时间复杂度: O(M),其中 M 是数组的最大元素。
辅助空间: O(M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。