|
| 1 | +// Runtime: 99 ms (Top 93.75%) | Memory: 76.80 MB (Top 6.6%) |
| 2 | + |
1 | 3 | class Cashier {
|
2 |
| - private Map<Integer, Integer> catalogue; |
3 |
| - |
4 |
| - private int n; |
5 |
| - private double discount; |
6 |
| - private int orderNumber; |
7 |
| - |
| 4 | + private final int[] prices; |
| 5 | + private final int n; |
| 6 | + private final int discount; |
| 7 | + private int customerNumber; |
| 8 | + |
8 | 9 | public Cashier(int n, int discount, int[] products, int[] prices) {
|
9 |
| - this.catalogue = new HashMap<>(); |
10 |
| - |
11 |
| - for (int i = 0; i < prices.length; i++) { |
12 |
| - this.catalogue.put(products[i], prices[i]); |
13 |
| - } |
14 |
| - |
| 10 | + this.prices = new int[200]; |
| 11 | + |
| 12 | + for(int i = 0; i < products.length; ++i) |
| 13 | + this.prices[products[i] - 1] = prices[i]; |
| 14 | + |
15 | 15 | this.n = n;
|
16 |
| - this.discount = ((double) 100 - discount)/100; |
17 |
| - this.orderNumber = 0; |
| 16 | + this.discount = discount; |
| 17 | + this.customerNumber = 1; |
18 | 18 | }
|
19 | 19 |
|
20 | 20 | public double getBill(int[] product, int[] amount) {
|
21 |
| - this.orderNumber++; |
22 |
| - |
23 |
| - double bill = 0.0; |
24 |
| - for (int i = 0; i < amount.length; i++) { |
25 |
| - int p = product[i]; |
26 |
| - int price = this.catalogue.get(p); |
27 |
| - bill += price*amount[i]; |
28 |
| - } |
29 |
| - |
30 |
| - if (this.orderNumber % n == 0) |
31 |
| - bill *= this.discount; |
32 |
| - |
33 |
| - return bill; |
| 21 | + double sum = 0; |
| 22 | + |
| 23 | + for(int i = 0; i < product.length; ++i) |
| 24 | + sum += this.prices[product[i] - 1] * amount[i]; |
| 25 | + |
| 26 | + if(this.customerNumber != 0 && this.customerNumber % n == 0) |
| 27 | + sum *= (double) (100 - this.discount) / 100; |
| 28 | + |
| 29 | + this.customerNumber++; |
| 30 | + |
| 31 | + return sum; |
34 | 32 | }
|
35 | 33 | }
|
36 | 34 |
|
|
0 commit comments