Leetcode Problem 1357. Apply Discount Every n Orders

1357. Apply Discount Every n Orders

Leetcode Solutions

Using a Product-Price Map and Customer Counter

  1. Initialize the Cashier class with the n, discount, products, and prices.
  2. Create a map (dictionary in Python) that maps product IDs to their prices for quick access.
  3. Initialize a customer counter to keep track of the number of customers served.
  4. When getBill is called, iterate over the product and amount arrays to calculate the subtotal.
  5. Check if the current customer is eligible for a discount by checking if the customer count is divisible by n.
  6. If eligible, apply the discount to the subtotal.
  7. Increment the customer counter.
  8. Return the final bill amount.
UML Thumbnail

Using a Product-Price Array and Modulo for Discount Application

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...