1357. Apply Discount Every n Orders Medium
1/**
2 * [1357] Apply Discount Every n Orders
3 *
4 * There is a supermarket that is frequented by many customers. The products sold at the supermarket are represented as two parallel integer arrays products and prices, where the i^th product has an ID of products[i] and a price of prices[i].
5 * When a customer is paying, their bill is represented as two parallel integer arrays product and amount, where the j^th product they purchased has an ID of product[j], and amount[j] is how much of the product they bought. Their subtotal is calculated as the sum of each amount[j] * (price of the j^th product).
6 * The supermarket decided to have a sale. Every n^th customer paying for their groceries will be given a percentage discount. The discount amount is given by discount, where they will be given discount percent off their subtotal. More formally, if their subtotal is bill, then they would actually pay bill * ((100 - discount) / 100).
7 * Implement the Cashier class:
8 *
9 * Cashier(int n, int discount, int[] products, int[] prices) Initializes the object with n, the discount, and the products and their prices.
10 * double getBill(int[] product, int[] amount) Returns the final total of the bill with the discount applied (if any). Answers within 10^-5 of the actual value will be accepted.
11 *
12 *
13 * Example 1:
14 *
15 * Input
16 * ["Cashier","getBill","getBill","getBill","getBill","getBill","getBill","getBill"]
17 * [[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]]]
18 * Output
19 * [null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]
20 * Explanation
21 * Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);
22 * cashier.getBill([1,2],[1,2]); // return 500.0. 1^st customer, no discount.
23 * // bill = 1 * 100 + 2 * 200 = 500.
24 * cashier.getBill([3,7],[10,10]); // return 4000.0. 2^nd customer, no discount.
25 * // bill = 10 * 300 + 10 * 100 = 4000.
26 * cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]); // return 800.0. 3^rd customer, 50% discount.
27 * // Original bill = 1600
28 * // Actual bill = 1600 * ((100 - 50) / 100) = 800.
29 * cashier.getBill([4],[10]); // return 4000.0. 4^th customer, no discount.
30 * cashier.getBill([7,3],[10,10]); // return 4000.0. 5^th customer, no discount.
31 * cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // return 7350.0. 6^th customer, 50% discount.
32 * // Original bill = 14700, but with
33 * // Actual bill = 14700 * ((100 - 50) / 100) = 7350.
34 * cashier.getBill([2,3,5],[5,3,2]); // return 2500.0. 6^th customer, no discount.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= n <= 10^4
40 * 0 <= discount <= 100
41 * 1 <= products.length <= 200
42 * prices.length == products.length
43 * 1 <= products[i] <= 200
44 * 1 <= prices[i] <= 1000
45 * The elements in products are unique.
46 * 1 <= product.length <= products.length
47 * amount.length == product.length
48 * product[j] exists in products.
49 * 1 <= amount[j] <= 1000
50 * The elements of product are unique.
51 * At most 1000 calls will be made to getBill.
52 * Answers within 10^-5 of the actual value will be accepted.
53 *
54 */
55pub struct Solution {}
56
57// problem: https://leetcode.com/problems/apply-discount-every-n-orders/
58// discuss: https://leetcode.com/problems/apply-discount-every-n-orders/discuss/?currentPage=1&orderBy=most_votes&query=
59
60// submission codes start here
61
62struct Cashier {
63 false
64 }
65
66
67/**
68 * `&self` means the method takes an immutable reference.
69 * If you need a mutable reference, change it to `&mut self` instead.
70 */
71impl Cashier {
72
73 fn new(n: i32, discount: i32, products: Vec<i32>, prices: Vec<i32>) -> Self {
74
75 }
76
77 fn get_bill(&self, product: Vec<i32>, amount: Vec<i32>) -> f64 {
78
79 }
80}
81
82/**
83 * Your Cashier object will be instantiated and called as such:
84 * let obj = Cashier::new(n, discount, products, prices);
85 * let ret_1: f64 = obj.get_bill(product, amount);
86 */
87
88// submission codes end
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn test_1357() {
96 }
97}
98
Back
© 2025 bowen.ge All Rights Reserved.