Leetcode 1357: Apply Discount Every n Orders

grid47
grid47
Exploring patterns and algorithms
Jun 24, 2024 6 min read

A supermarket offers various products, each with an associated price. The customers make purchases where the subtotal is calculated based on the prices and quantities of the products they buy. Every nth customer gets a discount on their subtotal, and the final bill is calculated accordingly.
Problem
Approach
Steps
Complexity
Input: The input consists of two parts: an initialization array to set up the cashier and a sequence of purchases made by customers.
Example: ["Cashier", "getBill", "getBill", "getBill", "getBill", "getBill", "getBill"] [[3, 50, [1, 2, 3, 4, 5], [100, 200, 300, 400, 500]], [[1, 2], [1, 2]], [[3, 5], [2, 2]], [[4], [5]], [[2, 3, 5], [3, 3, 3]], [[1, 5], [2, 5]], [[4, 3], [3, 4]]]
Constraints:
• 1 <= n <= 104
• 0 <= discount <= 100
• 1 <= products.length <= 200
• prices.length == products.length
• 1 <= products[i] <= 200
• 1 <= prices[i] <= 1000
• 1 <= product.length <= products.length
• amount.length == product.length
• product[j] exists in products
• 1 <= amount[j] <= 1000
• At most 1000 calls will be made to getBill.
Output: The output should return the final amount the customer needs to pay after applying the discount (if applicable).
Example: [null, 500.0, 1600.0, 400.0, 1900.0, 3000.0, 3200.0]
Constraints:
• The answer should be within 10^-5 of the actual value.
Goal: Calculate the subtotal based on the products and quantities, apply the discount for every nth customer, and return the final amount.
Steps:
• Initialize the Cashier class with n, discount, and product prices.
• For each customer, calculate the total price of the products purchased.
• If the customer is the nth customer, apply the discount to their subtotal.
• Return the final price after any discount.
Goal: The problem has several constraints related to the size of inputs and the number of operations.
Steps:
• The number of customers can go up to 10,000.
• The number of products can go up to 200.
• The number of calls to getBill will be at most 1,000.
Assumptions:
• Each customer is processed sequentially, and the discount is applied based on the order of their purchase.
Input: ["Cashier", "getBill", "getBill"] [[3, 50, [1, 2, 3], [100, 200, 300]], [[1, 2], [1, 1]], [[3], [5]]]
Explanation: In this example, the first two customers do not receive a discount, while the third customer, being the 3rd customer, gets a 50% discount.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus