Skip to content

Commit cbfbc39

Browse files
committed
Runtime: 99 ms (Top 93.75%) | Memory: 76.80 MB (Top 6.6%)
1 parent efe2760 commit cbfbc39

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

scripts/algorithms/A/Apply Discount Every n Orders/Apply Discount Every n Orders.java

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1+
// Runtime: 99 ms (Top 93.75%) | Memory: 76.80 MB (Top 6.6%)
2+
13
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+
89
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+
1515
this.n = n;
16-
this.discount = ((double) 100 - discount)/100;
17-
this.orderNumber = 0;
16+
this.discount = discount;
17+
this.customerNumber = 1;
1818
}
1919

2020
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;
3432
}
3533
}
3634

0 commit comments

Comments
 (0)