/**
* Problem: LeetCode 1357 - Apply Discount Every n Orders
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.HashMap;
import java.util.Map;
class Cashier {
private int n;
private int discount;
private int count;
private Map<Integer, Integer> priceMap;
public Cashier(int n, int discount, int[] products, int[] prices) {
this.n = n;
this.discount = discount;
this.count = 0;
priceMap = new HashMap<>();
for (int i = 0; i < products.length; i++) {
priceMap.put(products[i], prices[i]);
}
}
public double getBill(int[] product, int[] amount) {
count++;
long bill = 0;
for (int i = 0; i < product.length; i++) {
bill += (long) amount[i] * priceMap.get(product[i]);
}
// Har n-th customer ko discount milta hai
if (count % n == 0) {
return bill * (100 - discount) / 100.0;
}
return (double) bill;
}
}
/**
* Your Cashier object will be instantiated and called as such:
* Cashier obj = new Cashier(n, discount, products, prices);
* double param_1 = obj.getBill(product,amount);
*/
"""
Problem: LeetCode 1357 - Apply Discount Every n Orders
Difficulty: Medium
Language: Python 3
Author: CodeByTushu
"""
class Cashier:
def __init__(self, n: int, discount: int, products: list[int], prices: list[int]):
self.n = n
self.discount = discount
self.count = 0
self.price_map = {prod: price for prod, price in zip(products, prices)}
def getBill(self, product: list[int], amount: list[int]) -> float:
self.count += 1
bill = sum(amt * self.price_map[p] for p, amt in zip(product, amount))
if self.count % self.n == 0:
return bill * (100 - self.discount) / 100.0
return float(bill)
/**
* Problem: LeetCode 1357 - Apply Discount Every n Orders
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <unordered_map>
using namespace std;
class Cashier {
private:
int n;
int discount;
int count;
unordered_map<int, int> priceMap;
public:
Cashier(int n, int discount, vector<int>& products, vector<int>& prices) {
this->n = n;
this->discount = discount;
this->count = 0;
for (size_t i = 0; i < products.size(); i++) {
priceMap[products[i]] = prices[i];
}
}
double getBill(vector<int> product, vector<int> amount) {
count++;
long long bill = 0;
for (size_t i = 0; i < product.size(); i++) {
bill += (long long) amount[i] * priceMap[product[i]];
}
if (count % n == 0) {
return bill * (100.0 - discount) / 100.0;
}
return (double) bill;
}
};
/**
* Problem: LeetCode 1357 - Apply Discount Every n Orders
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number} n
* @param {number} discount
* @param {number[]} products
* @param {number[]} prices
*/
var Cashier = function(n, discount, products, prices) {
this.n = n;
this.discount = discount;
this.count = 0;
this.priceMap = new Map();
for (let i = 0; i < products.length; i++) {
this.priceMap.set(products[i], prices[i]);
}
};
/**
* @param {number[]} product
* @param {number[]} amount
* @return {number}
*/
Cashier.prototype.getBill = function(product, amount) {
this.count++;
let bill = 0;
for (let i = 0; i < product.length; i++) {
bill += amount[i] * this.priceMap.get(product[i]);
}
if (this.count % this.n === 0) {
return (bill * (100 - this.discount)) / 100;
}
return bill;
};
TIME COMPLEXITY
O(K)
SPACE COMPLEXITY
O(1)
TOPICS
Array
Data Stream
Data Structure
Ordered Set
Simulation