列表

详情


1357. 每隔 n 个顾客打折

超市里正在举行打折活动,每隔 n 个顾客会得到 discount 的折扣。

超市里有一些商品,第 i 种商品为 products[i] 且每件单品的价格为 prices[i] 。

结账系统会统计顾客的数目,每隔 n 个顾客结账时,该顾客的账单都会打折,折扣为 discount (也就是如果原本账单为 x ,那么实际金额会变成 x - (discount * x) / 100 ),然后系统会重新开始计数。

顾客会购买一些商品, product[i] 是顾客购买的第 i 种商品, amount[i] 是对应的购买该种商品的数目。

请你实现 Cashier 类:

 

示例 1:

输入
["Cashier","getBill","getBill","getBill","getBill","getBill","getBill","getBill"]
[[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]],[[1,2],[1,2]],[[3,7],[10,10]],[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]],[[4],[10]],[[7,3],[10,10]],[[7,5,3,1,6,4,2],[10,10,10,9,9,9,7]],[[2,3,5],[5,3,2]]]
输出
[null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]
解释
Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);
cashier.getBill([1,2],[1,2]);                        // 返回 500.0, 账单金额为 = 1 * 100 + 2 * 200 = 500.
cashier.getBill([3,7],[10,10]);                      // 返回 4000.0
cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]);    // 返回 800.0 ,账单原本为 1600.0 ,但由于该顾客是第三位顾客,他将得到 50% 的折扣,所以实际金额为 1600 - 1600 * (50 / 100) = 800 。
cashier.getBill([4],[10]);                           // 返回 4000.0
cashier.getBill([7,3],[10,10]);                      // 返回 4000.0
cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // 返回 7350.0 ,账单原本为 14700.0 ,但由于系统计数再次达到三,该顾客将得到 50% 的折扣,实际金额为 7350.0 。
cashier.getBill([2,3,5],[5,3,2]);                    // 返回 2500.0

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Cashier { public: Cashier(int n, int discount, vector<int>& products, vector<int>& prices) { } double getBill(vector<int> product, vector<int> amount) { } }; /** * 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); */

golang 解法, 执行用时: 244 ms, 内存消耗: 22.2 MB, 提交时间: 2022-12-11 12:55:07

type Cashier struct {
	num       int
	cousNum   int
	disciunt  int
	pricesMap map[int]int
}

func Constructor(n int, discount int, products []int, prices []int) Cashier {
	var res = Cashier{
		num:       n,
		cousNum:   0,
		disciunt:  discount,
		pricesMap: make(map[int]int, 10),
	}
	lenth := len(products)
	for i := 0; i < lenth; i++ {
		res.pricesMap[products[i]] = prices[i]
	}
	return res
}

func (this *Cashier) GetBill(product []int, amount []int) float64 {
	lenth := len(product)
	var sum = 0
	for i := 0; i < lenth; i++ {
		sum += amount[i] * this.pricesMap[product[i]]
	}
	var res float64
	this.cousNum++
	if this.cousNum%this.num == 0 {
		res = float64(sum) * float64(100-this.disciunt) / 100
	} else {
		res = float64(sum)
	}
	return res
}


/**
 * Your Cashier object will be instantiated and called as such:
 * obj := Constructor(n, discount, products, prices);
 * param_1 := obj.GetBill(product,amount);
 */

cpp 解法, 执行用时: 196 ms, 内存消耗: 117.4 MB, 提交时间: 2022-12-11 12:54:00

class Cashier {
private:
    unordered_map<int, int> price;
    int n, discount;
    int custom_id;
    
public:
    Cashier(int _n, int _d, vector<int>& products, vector<int>& prices): n(_n), discount(_d), custom_id(0) {
        for (int i = 0; i < products.size(); ++i) {
            price[products[i]] = prices[i];
        }
    }
    
    double getBill(vector<int> product, vector<int> amount) {
        ++custom_id;
        double payment = 0;
        for (int i = 0; i < product.size(); ++i) {
            payment += price[product[i]] * amount[i];
        }
        if (custom_id % n == 0) {
            payment -= payment * discount / 100;
        }
        return payment;
    }
};

/**
 * 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);
 */

python3 解法, 执行用时: 124 ms, 内存消耗: 22.5 MB, 提交时间: 2022-12-11 12:53:47

class Cashier:
    def __init__(self, n: int, discount: int, products: List[int], prices: List[int]):
        self.price = dict()
        for product, price in zip(products, prices):
            self.price[product] = price
        self.n = n
        self.discount = discount
        self.custom_id = 0

    def getBill(self, product: List[int], amount: List[int]) -> float:
        self.custom_id += 1
        payment = 0.0
        for k, v in zip(product, amount):
            payment += self.price[k] * v
        if self.custom_id % self.n == 0:
            payment -= payment * self.discount / 100
        return payment

# Your Cashier object will be instantiated and called as such:
# obj = Cashier(n, discount, products, prices)
# param_1 = obj.getBill(product,amount)

上一题