Skip to content

Commit 58abb75

Browse files
add 1357
1 parent d0e7d4b commit 58abb75

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1357|[Apply Discount Every n Orders](https://leetcode.com/problems/apply-discount-every-n-orders/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1357.java) | |Medium|Design|
1112
|1356|[Sort Integers by The Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1356.java) | |Easy|Sort, Bit Manipulation|
1213
|1354|[Construct Target Array With Multiple Sums](https://leetcode.com/problems/construct-target-array-with-multiple-sums/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1354.java) | |Hard|Greedy|
1314
|1353|[Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1353.java) | |Medium|Greedy, Sort, Segment Tree|
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1357. Apply Discount Every n Orders
8+
*
9+
* There is a sale in a supermarket, there will be a discount every n customer.
10+
* There are some products in the supermarket where the id of the i-th product is products[i] and the price per unit of this product is prices[i].
11+
* The system will count the number of customers and when the n-th customer arrive he/she will have a discount on the bill.
12+
* (i.e if the cost is x the new cost is x - (discount * x) / 100). Then the system will start counting customers again.
13+
* The customer orders a certain amount of each product where product[i] is the id of the i-th product the customer ordered and
14+
* amount[i] is the number of units the customer ordered of that product.
15+
*
16+
* Implement the Cashier class:
17+
* Cashier(int n, int discount, int[] products, int[] prices) Initializes the object with n, the discount, the products and their prices.
18+
* double getBill(int[] product, int[] amount) returns the value of the bill and apply the discount if needed. Answers within 10^-5 of the actual value will be accepted as correct.
19+
*
20+
* Example 1:
21+
* Input
22+
* ["Cashier","getBill","getBill","getBill","getBill","getBill","getBill","getBill"]
23+
* [[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]]]
24+
* Output
25+
* [null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]
26+
* Explanation
27+
* Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);
28+
* cashier.getBill([1,2],[1,2]); // return 500.0, bill = 1 * 100 + 2 * 200 = 500.
29+
* cashier.getBill([3,7],[10,10]); // return 4000.0
30+
* cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]); // return 800.0, The bill was 1600.0 but as this is the third customer, he has a discount of 50% which means his bill is only 1600 - 1600 * (50 / 100) = 800.
31+
* cashier.getBill([4],[10]); // return 4000.0
32+
* cashier.getBill([7,3],[10,10]); // return 4000.0
33+
* cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // return 7350.0, Bill was 14700.0 but as the system counted three more customers, he will have a 50% discount and the bill becomes 7350.0
34+
* cashier.getBill([2,3,5],[5,3,2]); // return 2500.0
35+
*
36+
*
37+
* Constraints:
38+
* 1 <= n <= 10^4
39+
* 0 <= discount <= 100
40+
* 1 <= products.length <= 200
41+
* 1 <= products[i] <= 200
42+
* There are not repeated elements in the array products.
43+
* prices.length == products.length
44+
* 1 <= prices[i] <= 1000
45+
* 1 <= product.length <= products.length
46+
* product[i] exists in products.
47+
* amount.length == product.length
48+
* 1 <= amount[i] <= 1000
49+
* At most 1000 calls will be made to getBill.
50+
* Answers within 10^-5 of the actual value will be accepted as correct.
51+
* */
52+
public class _1357 {
53+
public static class Solution1 {
54+
public static class Cashier {
55+
56+
int customerCount;
57+
int n;
58+
Map<Integer, Integer> productsToPrices;
59+
int discount;
60+
61+
public Cashier(int n, int discount, int[] products, int[] prices) {
62+
this.n = n;
63+
this.customerCount = 0;
64+
this.discount = discount;
65+
this.productsToPrices = new HashMap<>();
66+
for (int i = 0; i < products.length; i++) {
67+
productsToPrices.put(products[i], prices[i]);
68+
}
69+
}
70+
71+
public double getBill(int[] product, int[] amount) {
72+
double totalPrice = 0;
73+
for (int i = 0; i < product.length; i++) {
74+
totalPrice += productsToPrices.get(product[i]) * amount[i];
75+
}
76+
if (customerCount + 1 == n) {
77+
//apply discount
78+
totalPrice *= (double) (100 - discount) / 100;
79+
customerCount = 0;
80+
} else {
81+
customerCount++;
82+
}
83+
return totalPrice;
84+
}
85+
}
86+
}
87+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1357;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _1357Test {
9+
private static _1357.Solution1.Cashier cashier;
10+
11+
@Test
12+
public void test1() {
13+
cashier = new _1357.Solution1.Cashier(3, 50, new int[]{1, 2, 3, 4, 5, 6, 7}, new int[]{100, 200, 300, 400, 300, 200, 100});
14+
assertEquals(500.0, cashier.getBill(new int[]{1, 2}, new int[]{1, 2}), 0);
15+
assertEquals(4000.0, cashier.getBill(new int[]{3, 7}, new int[]{10, 10}), 0);
16+
assertEquals(800.0, cashier.getBill(new int[]{1, 2, 3, 4, 5, 6, 7}, new int[]{1, 1, 1, 1, 1, 1, 1}), 0);
17+
assertEquals(4000.0, cashier.getBill(new int[]{4}, new int[]{10}), 0);
18+
assertEquals(4000.0, cashier.getBill(new int[]{7, 3}, new int[]{10, 10}), 0);
19+
assertEquals(7350.0, cashier.getBill(new int[]{7, 5, 3, 1, 6, 4, 2}, new int[]{10, 10, 10, 9, 9, 9, 7}), 0);
20+
assertEquals(2500.0, cashier.getBill(new int[]{2, 3, 5}, new int[]{5, 3, 2}), 0);
21+
}
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy