Apply Discount Every n Orders - Problem
๐ Supermarket Cashier System
You're implementing a smart cashier system for a busy supermarket! The supermarket has decided to run an exciting promotion: every n-th customer gets a discount on their purchase.
Here's how it works:
- Each product has a unique
IDand aprice - When customers check out, they bring multiple products with different quantities
- Every n-th customer (3rd, 6th, 9th, etc.) gets a percentage discount on their total bill
- Other customers pay the full amount
Your task: Design a Cashier class that tracks customer orders and applies discounts automatically!
Example: If n=3 and discount=50%, then the 3rd, 6th, 9th customers get 50% off their bills.
The class needs two methods:
Cashier(n, discount, products[], prices[])- Initialize the systemgetBill(product[], amount[])- Calculate final bill for current customer
Input & Output
example_1.py โ Basic Usage
$
Input:
Cashier cashier = new Cashier(3, 50, [1,2,3,4,5,6,7], [100,200,300,400,500,600,700]);
cashier.getBill([1,2], [1,2]); // Customer 1
cashier.getBill([3,7], [10,10]); // Customer 2
cashier.getBill([1,2,3,4,5,6,7], [1,1,1,1,1,1,1]); // Customer 3
โบ
Output:
500.0
10000.0
1400.0
๐ก Note:
Customer 1: 1ร100 + 2ร200 = 500 (no discount). Customer 2: 10ร300 + 10ร700 = 10000 (no discount). Customer 3: 100+200+300+400+500+600+700 = 2800, but gets 50% discount = 1400.
example_2.py โ Small Discount
$
Input:
Cashier cashier = new Cashier(2, 10, [1,2], [5,3]);
cashier.getBill([1], [2]); // Customer 1
cashier.getBill([2], [3]); // Customer 2
โบ
Output:
10.0
8.1
๐ก Note:
Customer 1: 2ร5 = 10 (no discount). Customer 2: 3ร3 = 9, gets 10% discount = 9ร0.9 = 8.1.
example_3.py โ Multiple Discount Customers
$
Input:
Cashier cashier = new Cashier(3, 25, [1,2,3], [10,20,30]);
cashier.getBill([1], [1]); // Customer 1
cashier.getBill([2], [1]); // Customer 2
cashier.getBill([3], [1]); // Customer 3 (discount)
cashier.getBill([1,2], [1,1]); // Customer 4
cashier.getBill([2,3], [2,1]); // Customer 5
cashier.getBill([1,2,3], [1,1,1]); // Customer 6 (discount)
โบ
Output:
10.0
20.0
22.5
30.0
70.0
45.0
๐ก Note:
Customers 3 and 6 get 25% discount. Customer 3: 30ร0.75=22.5. Customer 6: (10+20+30)ร0.75=45.0.
Visualization
Tap to expand
Understanding the Visualization
1
System Setup
Store creates price lookup table and sets promotion rules (every nth customer gets discount)
2
Fast Checkout
Customer arrives with cart - system instantly looks up each item's price using hash map
3
Smart Discount
System checks customer count and automatically applies discount if they're the lucky nth customer
Key Takeaway
๐ฏ Key Insight: Hash maps transform O(m) searches into O(1) lookups, making the cashier system blazingly fast even with large product catalogs!
Time & Space Complexity
Time Complexity
O(k)
k is number of items in customer's cart - each price lookup is O(1) with hash map
โ Linear Growth
Space Complexity
O(m)
m is number of products in catalog - we store all product-price pairs in hash map
โ Linear Space
Constraints
- 1 โค n โค 104
- 0 โค discount โค 100
- 1 โค products.length โค 200
- 1 โค products[i] โค 200
- 1 โค prices[i] โค 1000
- products contains unique values
- 1 โค product.length โค products.length
- product[i] exists in products
- 1 โค amount[i] โค 1000
- At most 300 calls will be made to getBill
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code