Leetcode Problem 1357. Apply Discount Every n Orders
1357. Apply Discount Every n Orders
AI Mock Interview
Leetcode Solutions
Using a Product-Price Map and Customer Counter
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize the
Cashier
class with the
n
,
discount
,
products
, and
prices
.
Create a map (dictionary in Python) that maps product IDs to their prices for quick access.
Initialize a customer counter to keep track of the number of customers served.
When
getBill
is called, iterate over the
product
and
amount
arrays to calculate the subtotal.
Check if the current customer is eligible for a discount by checking if the customer count is divisible by
n
.
If eligible, apply the discount to the subtotal.
Increment the customer counter.
Return the final bill amount.
Using a Product-Price Array and Modulo for Discount Application
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...