Maximum Tip Calculator | Set 2
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received N orders. The amount of tips may differ when handled by different waiters and given as arrays A[] and B[] such that if Rahul takes the ith Order, he would be tipped A[i] rupees, and if Ankit takes this order, the tip would be B[i] rupees. In order to maximize the total tip value, they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints, Rahul cannot take more than X orders and Ankit cannot take more than Y orders. It is guaranteed that X + Y is greater than or equal to N, which means that all the orders can be handled by either Rahul or Ankit. The task is to find out the maximum possible amount of total tip money after processing all the orders.
Examples:
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 5: 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
Naive Approach: The naive approach to solve this article is discussed in the previous article.
Efficient Approach: The idea is to use the greedy technique to solve the problem. Sort the N orders in decreasing order on the basis of the difference between Rahul’s tip and Ankit’s tip. Then, traverse all the orders and take the maximum tip of the ith order if it is possible. Follow the steps below to solve the problem:
- Initialize ans as 0 to store the maximum possible tip.
- Create a vector of pair of integers, V of size N such that the first and second element of V[i] stores the tip of the ith order of Rahul and Ankit respectively.
- Sort the vector, V in decreasing order on the basis of the difference between the tips.
- Traverse the vector, V using the variable i
- If the value of Y is 0 OR the condition V[i].first ≥ V[i].second holds true, then add the value of V[i].first to ans and decrement X by 1.
- Otherwise, add the value of V[i].second to ans and decrement Y by 1.
- After completing the above steps, print the value of the ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool compare(pair< int , int > p1, pair< int , int > p2)
{
return abs (p1.first - p1.second)
> abs (p2.first - p2.second);
}
void maxTip( int A[], int B[], int X, int Y, int N)
{
vector<pair< int , int > > tips(N);
for ( int i = 0; i < N; i++) {
tips[i].first = A[i];
tips[i].second = B[i];
}
sort(tips.begin(), tips.end(), compare);
int ans = 0;
for ( int i = 0; i < N; i++) {
if (Y == 0
|| (X > 0 && tips[i].first >= tips[i].second)) {
ans += tips[i].first;
X--;
}
else {
ans += tips[i].second;
Y--;
}
}
cout << ans;
}
int main()
{
int N = 5, X = 3, Y = 3;
int A[] = { 1, 2, 3, 4, 5 };
int B[] = { 5, 4, 3, 2, 1 };
maxTip(A, B, X, Y, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Pair{
int x, y;
Pair( int a, int b){
x = a;
y = b;
}
}
class GFG {
public static void main (String[] args) {
int N = 5 , X = 3 , Y = 3 ;
int A[] = { 1 , 2 , 3 , 4 , 5 };
int B[] = { 5 , 4 , 3 , 2 , 1 };
int ans = maxTip(A, B, N, X, Y);
System.out.println(ans);
}
static int maxTip( int [] a, int [] b, int n, int x, int y) {
ArrayList<Pair> tips = new ArrayList<>();
for ( int i = 0 ; i < n; i++){
tips.add( new Pair(a[i], b[i]));
}
Collections.sort(tips, (a1, b1)-> {
return Math.abs(b1.x - b1.y) - Math.abs(a1.x - a1.y) ;
});
int ans = 0 ;
for ( int i = 0 ; i < n; i++){
Pair p = tips.get(i);
if (y == 0 || (p.x >= p.y && x > 0 )){
ans += p.x;
x--;
}
else if (y > 0 ) {
ans += p.y;
y--;
}
}
return ans;
}
}
|
Python3
from functools import cmp_to_key
def compare(p1, p2):
return abs (p2[ 0 ] - p2[ 1 ]) - abs (p1[ 0 ] - p1[ 1 ])
def maxTip(A, B, X, Y, N):
tips = []
for i in range (N):
tips.append((A[i], B[i]))
tips.sort(key = lambda x: - abs (x[ 0 ] - x[ 1 ]))
ans = 0
for i in range (N):
if Y = = 0 or (X > 0 and tips[i][ 0 ] > = tips[i][ 1 ]):
ans + = tips[i][ 0 ]
X - = 1
else :
ans + = tips[i][ 1 ]
Y - = 1
print (ans)
N, X, Y = 5 , 3 , 3
A = [ 1 , 2 , 3 , 4 , 5 ]
B = [ 5 , 4 , 3 , 2 , 1 ]
maxTip(A, B, X, Y, N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Pair
{
public int x, y;
public Pair( int a, int b)
{
x = a;
y = b;
}
}
class GFG
{
static void Main( string [] args)
{
int N = 5, X = 3, Y = 3;
int [] A = { 1, 2, 3, 4, 5 };
int [] B = { 5, 4, 3, 2, 1 };
int ans = maxTip(A, B, N, X, Y);
Console.WriteLine(ans);
}
static int maxTip( int [] a, int [] b, int n, int x, int y)
{
List<Pair> tips = new List<Pair>();
for ( int i = 0; i < n; i++)
{
tips.Add( new Pair(a[i], b[i]));
}
tips.Sort((a1, b1) => Math.Abs(b1.x - b1.y) - Math.Abs(a1.x - a1.y));
int ans = 0;
for ( int i = 0; i < n; i++)
{
Pair p = tips[i];
if (y == 0 || (p.x >= p.y && x > 0))
{
ans += p.x;
x--;
}
else if (y > 0)
{
ans += p.y;
y--;
}
}
return ans;
}
}
|
Javascript
<script>
function compare(p1, p2)
{
return Math.abs(p2[0] - p2[1]) - Math.abs(p1[0] - p1[1]);
}
function maxTip(A, B, X, Y, N)
{
let tips = new Array(N);
for (let i = 0; i < N; i++) {
tips[i] = new Array();
tips[i][0] = A[i];
tips[i][1] = B[i];
}
tips.sort(compare);
let ans = 0;
for (let i = 0; i < N; i++) {
if (Y == 0
|| (X > 0 && tips[i][0] >= tips[i][1])) {
ans += tips[i][0];
X--;
}
else {
ans += tips[i][1];
Y--;
}
}
document.write(ans, "</br>" );
}
let N = 7, X = 3, Y = 4;
let A = [8, 7, 15, 19, 16, 16, 18];
let B = [1, 7, 15, 11, 12, 31, 9];
maxTip(A, B, X, Y, N);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...