You have $50 and a list of n food items, the list has the name of each item and beside it it's price and how many hours it will last when you eat it, you want to buy exactly 2 items that combined together will last the maximum number of hours while costing $50 or less...
The list you are given has the items ordered by price, how can you use that information to come up with an algorithm to find the best pair in O(n) time complexity (instead of O(n²))
Question
Guest
You have $50 and a list of n food items, the list has the name of each item and beside it it's price and how many hours it will last when you eat it, you want to buy exactly 2 items that combined together will last the maximum number of hours while costing $50 or less...
The list you are given has the items ordered by price, how can you use that information to come up with an algorithm to find the best pair in O(n) time complexity (instead of O(n²))
Link to comment
Share on other sites
6 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.