拉胡尔和安吉特是皇家餐厅仅有的两名服务员。今天,餐厅接到N个订单。当由不同的服务员处理并作为数组A[]和B[]给出时,小费的数量可能会有所不同,这样如果Rahul接受第i个订单,他将获得A[i]卢比的小费,如果Ankit接受这个订单,小费是B[i]卢比。
为了最大化总小费价值,他们决定在自己之间分配订单。一份订单将只由一个人处理。此外,由于时间限制,Rahul 不能接受多于X 个订单,而 Ankit 不能接受多于Y 个订单。保证X + Y大于或等于N ,这意味着所有订单都可以由 Rahul 或 Ankit 处理。任务是在处理完所有订单后找出最大可能的总小费金额。
例子:
Input: N = 5, X = 3, Y = 3, A[] = {1, 2, 3, 4, 5}, B[] = {5, 4, 3, 2, 1}
Output: 21
Explanation:
Step 1: 5 is included from Ankit’s array
Step 2: 4 is included from Ankit’s array
Step 3: As both of them has same value 3 then choose any one of them
Step 4: 4 is included from Rahul’s array
Step 4: 5 is included from Rahul’s array
Therefore, the maximum possible amount of total tip money sums up to 21.
Input: N = 7, X = 3, Y = 4, A[] = {8, 7, 15, 19, 16, 16, 18}, B[] = {1, 7, 15, 11, 12, 31, 9}
Output: 110
朴素的方法:最简单的方法是遍历给定的数组并同时开始遍历两个数组并选择其中最大的元素,如果元素取自X则减少X的计数,否则减少Y的计数。如果X或Y 之一变为0 ,则遍历其他非零数组并将其值添加到最大利润。在每一步中,都有一个选择,这类似于 0-1 背包问题,其中决定是包含还是排除一个元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that finds the maximum tips
// from the given arrays as per the
// given conditions
int maximumTip(vector &arr1,vector & arr2,
int n, int x, int y)
{
// Base Condition
if (n == 0)
return 0;
// If both have non-zero count then
// return max element from both array
if (x != 0 and y != 0)
return max(
arr1[n - 1] + maximumTip(arr1, arr2, n - 1,
x - 1, y),
arr2[n - 1] + maximumTip(arr1, arr2, n - 1, x,
y - 1));
// Traverse first array, as y
// count has become 0
if (y == 0)
return arr1[n - 1] + maximumTip(arr1, arr2, n - 1,
x - 1, y);
// Traverse 2nd array, as x
// count has become 0
else
return arr2[n - 1] + maximumTip(arr1, arr2, n - 1,
x, y - 1);
}
// Drive Code
int main()
{
int N = 5;
int X = 3;
int Y = 3;
vector A = { 1, 2, 3, 4, 5 };
vector B = { 5, 4, 3, 2, 1 };
// Function Call
cout << (maximumTip(A, B, N, X, Y));
}
// This code is contributed by mohit kumar 29
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Function that finds the maximum tips
// from the given arrays as per the
// given conditions
static int maximumTip(int []arr1,int []arr2,
int n, int x, int y)
{
// Base Condition
if (n == 0)
return 0;
// If both have non-zero count then
// return max element from both array
if (x != 0 && y != 0)
return Math.max(
arr1[n - 1] + maximumTip(arr1, arr2, n - 1,
x - 1, y),
arr2[n - 1] + maximumTip(arr1, arr2, n - 1, x,
y - 1));
// Traverse first array, as y
// count has become 0
if (y == 0)
return arr1[n - 1] + maximumTip(arr1, arr2, n - 1,
x - 1, y);
// Traverse 2nd array, as x
// count has become 0
else
return arr2[n - 1] + maximumTip(arr1, arr2, n - 1,
x, y - 1);
}
// Drive Code
public static void main (String[] args) {
int N = 5;
int X = 3;
int Y = 3;
int A[] = { 1, 2, 3, 4, 5 };
int B[] = { 5, 4, 3, 2, 1 };
// Function Call
System.out.println(maximumTip(A, B, N, X, Y));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function that finds the maximum tips
# from the given arrays as per the
# given conditions
def maximumTip(arr1, arr2, n, x, y):
# Base Condition
if n == 0:
return 0
# If both have non-zero count then
# return max element from both array
if x != 0 and y != 0:
return max(
arr1[n-1] + maximumTip(arr1, arr2, n - 1, x-1, y),
arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1)
)
# Traverse first array, as y
# count has become 0
if y == 0:
return arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y)
# Traverse 2nd array, as x
# count has become 0
else:
return arr2[n - 1] + maximumTip(arr1, arr2, n-1, x, y-1)
# Drive Code
N = 5
X = 3
Y = 3
A = [1, 2, 3, 4, 5]
B = [5, 4, 3, 2, 1]
# Function Call
print(maximumTip(A, B, N, X, Y))
Javascript
Java
// Java program for the above approach
import java.io.*;
import java.util.HashMap;
class GFG {
// Function that finds the maximum tips
// from the given arrays as per the
// given conditions
static int maximumTip(int[] arr1, int[] arr2, int n,
int x, int y,
HashMap dd)
{
// Create key of N, X, Y
String key = Integer.toString(n) + "_"
+ Integer.toString(x) + "_"
+ Integer.toString(y);
// Return if the current state is
// already calculated
if (dd.get(key) != null)
return dd.get(key);
// Base Condition
if (n == 0)
return 0;
// If both have non-zero count then store and
// return max element from both array
if (x != 0 && y != 0) {
int temp = Math.max(
arr1[n - 1]
+ maximumTip(arr1, arr2, n - 1, x - 1,
y, dd),
arr2[n - 1]
+ maximumTip(arr1, arr2, n - 1, x,
y - 1, dd));
dd.put(key, temp);
// Return the current state result
return dd.get(key);
}
// if y is zero, only x value
// can be used
if (y == 0) {
int temp = arr1[n - 1]
+ maximumTip(arr1, arr2, n - 1,
x - 1, y, dd);
dd.put(key, temp);
// Return the current state result
return dd.get(key);
}
// if x is zero, only y value
// can be used
else {
int temp = arr2[n - 1]
+ maximumTip(arr1, arr2, n - 1, x,
y - 1, dd);
dd.put(key, temp);
// Return the current state result
return dd.get(key);
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int X = 3;
int Y = 3;
int A[] = { 1, 2, 3, 4, 5 };
int B[] = { 5, 4, 3, 2, 1 };
// Stores the results of the
// overlapping state
HashMap dd
= new HashMap();
// Function Call
System.out.println(maximumTip(A, B, N, X, Y, dd));
}
}
// This code is contributed by MuskanKalra1
Python3
# Python program for the above approach
# Function that finds the maximum tips
# from the given arrays as per the
# given conditions
def maximumTip(arr1, arr2, n, x, y, dd):
# Create key of N, X, Y
key = str(n) + '_' + str(x) + '_' + str(y)
# Return if the current state is
# already calculated
if key in dd:
return dd[key]
# Base Condition
if n == 0:
return 0
# Store and return
if x != 0 and y != 0:
dd[key] = max(
arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd),
arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)
)
# Return the current state result
return dd[key]
# If y is zero, only x value
# can be used
if y == 0:
dd[key] = arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd)
# Return the current state result
return dd[key]
# If x is zero, only y value
# can be used
else:
dd[key] = arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)
# Return the current state result
return dd[key]
# Drive Code
N = 5
X = 3
Y = 3
A = [1, 2, 3, 4, 5]
B = [5, 4, 3, 2, 1]
# Stores the results of the
# overlapping state
dd = {}
# Function Call
print(maximumTip(A, B, N, X, Y, dd))
C++
#include
using namespace std;
int dp[1001][101][101];
int rec(int level, int x, int y, int arr1[], int arr2[],
int n)
{
if (level == n)
return 0;
if (x == 0 && y == 0)
return 0;
if (x == 0)
return arr2[level]
+ rec(level + 1, x, y - 1, arr1, arr2, n);
if (y == 0)
return arr1[level]
+ rec(level + 1, x - 1, y, arr1, arr2, n);
if (dp[level][x][y] != -1)
return dp[level][x][y];
int ans = max(rec(level + 1, x - 1, y, arr1, arr2, n)
+ arr1[level],
rec(level + 1, x, y - 1, arr1, arr2, n)
+ arr2[level]);
return dp[level][x][y] = ans;
}
void solve()
{
int n = 7, x = 3, y = 4;
int arr1[] = { 8, 7, 15, 19, 16, 16, 18 },
arr2[] = { 1, 7, 15, 11, 12, 31, 9 };
memset(dp, -1, sizeof(dp));
cout << rec(0, x, y, arr1, arr2, n);
}
int main()
{
solve();
return 0;
}
21
时间复杂度: O(2 N )
辅助空间: O(1)
高效的方法:可以通过使用动态规划和记忆化来优化上述方法。如果针对N 、 X 、 Y的值跟踪执行,则可以看出是否存在重叠子问题。这些重叠的子问题可以计算一次并在递归调用中调用相同的子问题时存储和使用。以下是步骤:
- 初始化 Map/Dictionary 以存储重叠子问题的结果。地图的键将是N 、 X和Y 的组合值。
- 在每次递归调用时,检查映射中是否存在给定的键,然后从映射本身返回值。
- 否则,递归调用该函数并将值存储在映射中并返回存储的值。
- 如果X和Y非零,则递归调用函数并取使用X时和使用Y时返回值的最大值。
- 如果X或Y为零,则递归调用非零数组。
- 在上述递归调用结束后,打印计算出的最大可能的提示量。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.io.*;
import java.util.HashMap;
class GFG {
// Function that finds the maximum tips
// from the given arrays as per the
// given conditions
static int maximumTip(int[] arr1, int[] arr2, int n,
int x, int y,
HashMap dd)
{
// Create key of N, X, Y
String key = Integer.toString(n) + "_"
+ Integer.toString(x) + "_"
+ Integer.toString(y);
// Return if the current state is
// already calculated
if (dd.get(key) != null)
return dd.get(key);
// Base Condition
if (n == 0)
return 0;
// If both have non-zero count then store and
// return max element from both array
if (x != 0 && y != 0) {
int temp = Math.max(
arr1[n - 1]
+ maximumTip(arr1, arr2, n - 1, x - 1,
y, dd),
arr2[n - 1]
+ maximumTip(arr1, arr2, n - 1, x,
y - 1, dd));
dd.put(key, temp);
// Return the current state result
return dd.get(key);
}
// if y is zero, only x value
// can be used
if (y == 0) {
int temp = arr1[n - 1]
+ maximumTip(arr1, arr2, n - 1,
x - 1, y, dd);
dd.put(key, temp);
// Return the current state result
return dd.get(key);
}
// if x is zero, only y value
// can be used
else {
int temp = arr2[n - 1]
+ maximumTip(arr1, arr2, n - 1, x,
y - 1, dd);
dd.put(key, temp);
// Return the current state result
return dd.get(key);
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int X = 3;
int Y = 3;
int A[] = { 1, 2, 3, 4, 5 };
int B[] = { 5, 4, 3, 2, 1 };
// Stores the results of the
// overlapping state
HashMap dd
= new HashMap();
// Function Call
System.out.println(maximumTip(A, B, N, X, Y, dd));
}
}
// This code is contributed by MuskanKalra1
蟒蛇3
# Python program for the above approach
# Function that finds the maximum tips
# from the given arrays as per the
# given conditions
def maximumTip(arr1, arr2, n, x, y, dd):
# Create key of N, X, Y
key = str(n) + '_' + str(x) + '_' + str(y)
# Return if the current state is
# already calculated
if key in dd:
return dd[key]
# Base Condition
if n == 0:
return 0
# Store and return
if x != 0 and y != 0:
dd[key] = max(
arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd),
arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)
)
# Return the current state result
return dd[key]
# If y is zero, only x value
# can be used
if y == 0:
dd[key] = arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd)
# Return the current state result
return dd[key]
# If x is zero, only y value
# can be used
else:
dd[key] = arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)
# Return the current state result
return dd[key]
# Drive Code
N = 5
X = 3
Y = 3
A = [1, 2, 3, 4, 5]
B = [5, 4, 3, 2, 1]
# Stores the results of the
# overlapping state
dd = {}
# Function Call
print(maximumTip(A, B, N, X, Y, dd))
C++
#include
using namespace std;
int dp[1001][101][101];
int rec(int level, int x, int y, int arr1[], int arr2[],
int n)
{
if (level == n)
return 0;
if (x == 0 && y == 0)
return 0;
if (x == 0)
return arr2[level]
+ rec(level + 1, x, y - 1, arr1, arr2, n);
if (y == 0)
return arr1[level]
+ rec(level + 1, x - 1, y, arr1, arr2, n);
if (dp[level][x][y] != -1)
return dp[level][x][y];
int ans = max(rec(level + 1, x - 1, y, arr1, arr2, n)
+ arr1[level],
rec(level + 1, x, y - 1, arr1, arr2, n)
+ arr2[level]);
return dp[level][x][y] = ans;
}
void solve()
{
int n = 7, x = 3, y = 4;
int arr1[] = { 8, 7, 15, 19, 16, 16, 18 },
arr2[] = { 1, 7, 15, 11, 12, 31, 9 };
memset(dp, -1, sizeof(dp));
cout << rec(0, x, y, arr1, arr2, n);
}
int main()
{
solve();
return 0;
}
21
时间复杂度: O(N*X*Y)
辅助空间: O(N*X*Y)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。