Recipe Shopping Cart

Asked at Decagon· Screen

Reported by the problem's creator, not verified

Import another question

You are building RecipeCart for a cooking app. The constructor receives recipes, which maps each recipe name to its list of ingredients, and discounts, which maps each discounted ingredient to its tiers. A tier [threshold, amount] means the ingredient earns amount dollars off when at least threshold recipes in the cart use it. Among all tiers whose threshold is met, the highest threshold's amount applies. Ingredients not in discounts earn nothing. The cart holds at most one copy of each recipe and starts empty.

Examples

  1. Example 1

    Input: init = [{"Aioli":["Garlic","Egg"],"Stir Fry":["Chicken","Garlic","Ginger"],"Chicken Parm":["Chicken","Garlic"],"Garlic Bread":["Bread","Garlic"]},{"Garlic":[[2,3],[4,8]]}], operations = [["add_recipe",["Chicken Parm"]],["get_total_discount",[]],["add_recipe",["Stir Fry"]],["get_total_discount",[]],["add_recipe",["Garlic Bread"]],["get_total_discount",[]],["add_recipe",["Aioli"]],["get_total_discount",[]],["remove_recipe",["Stir Fry"]],["get_total_discount",[]]]

    Output: [null,0,null,3,null,3,null,8,null,3]

    Explanation: After Chicken Parm, Garlic is used by one recipe, so no tier is met and the total is 0. Stir Fry raises Garlic's count to 2, meeting the [2, 3] tier. Garlic Bread raises it to 3, still $3. Aioli raises it to 4, meeting [4, 8]. Removing Stir Fry drops the count back to 3, so the total returns to $3.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.