Number of ways to buy all type of food items with X dollars.

Revision en1, by OneWhoKnocks, 2020-02-09 13:21:01

George went to a shop and is planning to buy food for his family. There are 4 members in his family including himself. And everyone wants to eat something different.

He wants Pizza for himself, his wife just wants salad , his son wants to eat hamburger and his daughter wants to have a burrito for herself.

In the shop there are multiple varieties of each food type, find the number of ways in which George can buy food for each person in the family(same food type as they want) if he has X dollars in his pocket.

Eg:

Prices of each food:

Pizza [1, 4, 5]

Salad [2, 3, 6]

Hamburger [5]

Burrito [6 3 3]

My solution: Sort each list. Find number of ways iterating through each list and doing binary search on the fourth one.

Is there any better solution than this? Thanks.

Constraints: 1 <= x <= 10^9 1 <= size of each list <= 10^3 1 <= price of food item <= 10^9

Tags #sorting, #recursion, #array, #combinatorics

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English OneWhoKnocks 2020-02-09 13:21:01 992 Initial revision (published)