Cheapest Available Menu Items
At Zomato, presenting relevant and attractive options to users is crucial for a great dining experience. One common scenario is highlighting the most affordable dishes from a restaurant that are currently available. This helps users quickly find budget-friendly choices and encourages more orders, especially during peak hours or for quick selections. This problem focuses on efficiently identifying and presenting these top cheapest items.
Your task is to implement a function that takes a list of menu items and an integer N, and returns the N cheapest menu items that are currently marked as available.
Specifically, you must:
menuItems array to include only those items where available is true.price in ascending order.N items from the sorted list. If there are fewer than N available items, return all of them.Function Signature:
function getCheapestAvailableItems(menuItems: MenuItem[], N: number): MenuItem[]
Input Parameters:
menuItems: An array of MenuItem objects. Each MenuItem object has the following structure:
id: string - A unique identifier for the menu item.name: string - The name of the dish.price: number - The price of the dish.available: boolean - Indicates whether the dish is currently available.N: number - The maximum number of cheapest items to return.Return Value:
MenuItem objects, sorted by price (ascending), representing up to N cheapest available items. If no items are available or N is 0, an empty array should be returned.menuItems is an empty array, the function should return an empty array.N is zero: If N is 0, the function should return an empty array.Examples
Example 1
Input:
menuItems = [{"id": "1", "name": "Burger", "price": 150, "available": true}, {"id": "2", "name": "Fries", "price": 80, "available": true}, {"id": "3", "name": "Coke", "price": 50, "available": false}, {"id": "4", "name": "Pizza", "price": 300, "available": true}], N = 2
Output:
[{"id": "2", "name": "Fries", "price": 80, "available": true}, {"id": "1", "name": "Burger", "price": 150, "available": true}]
Note:
Coke is unavailable. Among the available items (Fries, Burger, Pizza), Fries (80) and Burger (150) are the two cheapest.
Example 2
Input:
menuItems = [{"id": "1", "name": "Salad", "price": 200, "available": false}, {"id": "2", "name": "Soup", "price": 100, "available": true}, {"id": "3", "name": "Bread", "price": 50, "available": false}], N = 3
Output:
[{"id": "2", "name": "Soup", "price": 100, "available": true}]
Note:
Only Soup is available. Since N=3 but only 1 item is available, the function should return the single available item.
Example 3
Input:
menuItems = [], N = 5
Output:
[]
Note:
The input menuItems array is empty, so an empty array is returned.
Example 4
Input:
menuItems = [{"id": "1", "name": "Pasta", "price": 250, "available": true}, {"id": "2", "name": "Garlic Bread", "price": 120, "available": true}], N = 0
Output:
[]
Note:
Since N is 0, no items should be returned.
Constraints
0 <= menuItems.length <= 10^50 <= N <= menuItems.lengthmenuItems[i].id is a unique string.menuItems[i].name is a string.1 <= menuItems[i].price <= 1000 (prices can be integers or floats).menuItems[i].available is a boolean.10^5 menu items within typical time limits.N=0, or fewer available items than N?Loading editor...