Dynamic Programming 15

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, if Rahul takes the ith order, he would be tipped ai rupees and if Ankit takes this order, the tip would be bi 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. Find out the maximum possible amount of total tip money after processing all the orders.

Problem link :- click here


Code



import java.util.HashMap;
import java.util.Scanner;

public class DynamicProgramming_15
{
	//Recursion
	static int maxTip(int n, int a[], int b[], int x, int y)
	{
		if(n == 0)
			return 0;
		
		if(x==0  &&  y>0)
		{
			int sum = 0;
			for(int i=0; i<n; i++)
				sum += b[i];
			return sum;
		}
		
		if(x>0  &&  y==0)
		{
			int sum = 0;
			for(int i=0; i<n; i++)
				sum += a[i];
			return sum;
		}
		
		int r_tip = a[n-1] + maxTip(n-1, a, b,   x-1,  y    );
		int a_tip = b[n-1] + maxTip(n-1, a, b,  x,     y-1);
		
		return Math.max(r_tip, a_tip);
	}
	
	//Memoization
	//Till now we have one value to store for `one` index
	//in fibonacci fib(5) has some value which we have stored in a array with index 5
	//But here we have store value for unique appearance of `x`, `y`, `n`
	// So we are converting them to strings and store it in a hash-map
	// where x, y, n ---> key		&	maxTip(x,y,n) ---> value
	static int maxTip1(int n, int a[], int b[], int x, int y, HashMap<String, Integer> dp)
	{
		String key = Integer.toString(x) + "-" + Integer.toString(y) + "-" + Integer.toString(n);
		
		if(dp.containsKey(key))
			return dp.get(key);
		
		if(n == 0)
			return 0;
		
		if(x==0  &&  y>0)
		{
			int sum = 0;
			for(int i=0; i<n; i++)
				sum += b[i];
			
			dp.put(key, sum);
			return sum;
		}
		
		if(x>0  &&  y==0)
		{
			int sum = 0;
			for(int i=0; i<n; i++)
				sum += a[i];

			dp.put(key, sum);
			return sum;
		}
		
		int r_tip = a[n-1] + maxTip(n-1, a, b,   x-1,  y    );
		int a_tip = b[n-1] + maxTip(n-1, a, b,  x,     y-1);
		
		int ans = Math.max(r_tip, a_tip);
		dp.put(key, ans);
		return ans;
	}

	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		
		System.out.print("Enter the number of orders of the day : ");
		int n = s.nextInt();
		int a[] = new int[n];
		int b[] = new int[n];
		
		System.out.println("Enter the tip array of Rahul : ");
		for(int i=0; i<n; i++)
			a[i] = s.nextInt();
		
		System.out.println("Enter the tip array of Ankit : ");
		for(int i=0; i<n; i++)
			b[i] = s.nextInt();
		
		System.out.print("Enter the maximum orders that can be taken by Rahul : ");
		int x = s.nextInt();
		
		System.out.print("Enter the maximum orders that can be taken by Ankit : ");
		int y = s.nextInt();
		
		HashMap<String, Integer> dp = new HashMap<>();
		int ans = maxTip1(n, a, b, x, y, dp);
		System.out.println("\n\n" + ans);
		
		s.close();
	}

}



Comments