[英]Maximum tip calculator - naive solution
I am working through a Geekforgeeks practice question.我正在解决 Geekforgeeks 练习题。 I have come up with a naive recursive solution to the "maximum tip calculator" problem.
我想出了一个简单的递归解决方案来解决“最大小费计算器”问题。
The problem definition is:问题定义是:
Restaurant recieves N orders.
餐厅收到 N 个订单。 If Rahul takes the ith order, gain $A[i].
如果 Rahul 取第 i 个订单,则获得 $A[i]。 If Ankit takes this order, the tip would be $B[i] One order per person.
如果 Ankit 接受此订单,小费将是 $B[i] 每人一份订单。 Rahul takes max X orders.
Rahul 最多接受 X 个订单。 Ankit takes max Y orders.
Ankit 最多接受 Y 个订单。 X + Y >= N. Find out the maximum possible amount of total tip money after processing all the orders.
X + Y >= N. 在处理完所有订单后找出最大可能的总小费金额。
Input:
输入:
The first line contains one integer, number of test cases.
第一行包含一个整数,测试用例的数量。 The second line contains three integers N, X, Y. The third line contains N integers.
第二行包含三个整数 N、X、Y。第三行包含 N 个整数。 The ith integer represents Ai.
第 i 个整数代表 Ai。 The fourth line contains N integers.
第四行包含 N 个整数。 The ith integer represents Bi.
第 i 个整数代表 Bi。
Output: Print a single integer representing the maximum tip money they would receive.
输出:打印一个整数,代表他们将收到的最大小费。
My Code and working sample:我的代码和工作示例:
def max_tip(N, A, B, X, Y, n= 0):
if n == len(A) or N == 0:
return 0
if X == 0 and Y > 0: # rahul cannot take more orders
return max(B[n] + max_tip(N - 1, A, B, X, Y - 1, n + 1), # ankit takes the order
max_tip(N, A, B, X, Y, n + 1)) # ankit does not take order
elif Y == 0 and X > 0: # ankit cannot take more orders
return max(A[n] + max_tip(N - 1, A, B, X - 1, Y, n + 1), # rahul takes the order
max_tip(N, A, B, X, Y, n + 1)) # rahul does not take order
elif Y == 0 and X == 0: # neither can take orders
return 0
else:
return max(A[n] + max_tip(N - 1, A, B, X - 1, Y, n + 1), # rahul takes the order
B[n] + max_tip(N - 1, A, B, X, Y - 1, n + 1), #ankit takes the order
max_tip(N, A, B, X, Y, n + 1)) # nobody takes the order
T = int(input())
for i in range(T):
nxy = [int(n) for n in input().strip().split(" ")]
N = nxy[0]
X = nxy[1]
Y = nxy[2]
A = [int(n) for n in input().strip().split(" ")]
B = [int(n) for n in input().strip().split(" ")]
print(max_tip(N, A, B, X, Y))
I've annotated my recursive call decisions.我已经注释了我的递归调用决策。 Essentially I extended the naive solution for 0-1 knapsack in another dimension two waiters, either one takes, the other takes, or both do not take the order depending on the orders left constraint.
本质上,我在另一个维度中扩展了 0-1 背包的幼稚解决方案,两个服务员,一个拿,另一个拿,或者两个都不拿订单,这取决于订单留下的约束。
The solution checker is complaining for the following testcase:解决方案检查器抱怨以下测试用例:
Input:
7 3 3
8 7 15 19 16 16 18
1 7 15 11 12 31 9
Its Correct output is:
110
And Your Code's Output is:
106
This confuses me because the optimal solution seems to be what my code is getting (19 + 16 + 18) + (7 + 15 + 31).这让我感到困惑,因为最佳解决方案似乎是我的代码得到的 (19 + 16 + 18) + (7 + 15 + 31)。 The immediate issue seems to be that X + Y < N. My thought is my code should work for the case where X + Y < N as well.
眼前的问题似乎是 X + Y < N。我的想法是我的代码也应该适用于 X + Y < N 的情况。
What's going on?这是怎么回事?
you are considering the case, where nobody takes the tip.您正在考虑没有人接受小费的情况。 But that case doesn't exist as X+Y >= n.
但是这种情况不存在,因为 X+Y >= n。 This java code worked for me, have a look.
这个java代码对我有用,看看。
private static int getMaxTip(int x, int y, int n, int[] A, int[] B) {
int[][] dp = new int[x + 1][y + 1];
dp[0][0] = 0;
for (int i = 1;i <= x;i++) {
dp[i][0] = (i <= n) ? dp[i - 1][0] + A[i - 1] : dp[i - 1][0];
}
for (int i = 1;i <= y;i++) {
dp[0][i] = (i <= n) ? dp[0][i - 1] + B[i - 1] : dp[0][i - 1];
}
for (int i = 1;i <= x;i++) {
for (int j = 1;j <= y;j++) {
if (i + j <= n) {
dp[i][j] = Math.max(dp[i - 1][j] + A[i + j - 1], dp[i][j - 1] + B[i + j - 1]);
}
}
}
int ans = Integer.MIN_VALUE;
for (int i = 0;i <= x;i++) {
for (int j = 0;j <= y;j++) {
if (i + j == n) {
ans = Math.max(ans, dp[i][j]);
}
}
}
return ans;
}
您正在考虑这样一种情况,即没有人接受不应考虑的订单,因为问题中提到了 x+y>=n always.Removing the condition will work.
I am assuming, this is your source of question: https://practice.geeksforgeeks.org/problems/maximum-tip-calculator/0我假设,这是您的问题来源: https : //practice.geeksforgeeks.org/problems/maximum-tip-calculator/0
Here is my solution written in Python that passed all case: https://github.com/Madhu-Guddana/My-Solutions/blob/master/adhoc/max_tip.py这是我用 Python 编写的解决方案,通过了所有案例: https : //github.com/Madhu-Guddana/My-Solutions/blob/master/adhoc/max_tip.py
Explanation: zip corresponding element of tips and create new array.说明:zip 对应的tip 元素并创建新数组。 Sort the new array based on difference amount value for Rahul and Ankit, Then we can safely consider the elements from 2 ends of the array, which ever end is giving more profit, add the value to count.
根据 Rahul 和 Ankit 的差异量值对新数组进行排序,然后我们可以安全地考虑数组两端的元素,哪一端收益更多,将值相加进行计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.