-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
4,079 additions
and
2,462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Apply Discount Every n Orders | ||
|
||
LeetCode #: [1357](https://leetcode.com/problems/apply-discount-every-n-orders/) | ||
|
||
Difficulty: Medium | ||
|
||
Topics: Design. | ||
|
||
## Problem | ||
|
||
There is a sale in a supermarket, there will be a `discount` every `n` customer. | ||
|
||
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]`. | ||
|
||
The system will count the number of customers and when the `n-th` customer arrive he/she will have a `discount` on the bill. (i.e if the cost is `x` the new cost is `x - (discount * x) / 100`). Then the system will start counting customers again. | ||
|
||
The customer orders a certain amount of each product where `product[i]` is the id of the `i-th` product the customer ordered and `amount[i]` is the number of units the customer ordered of that product. | ||
|
||
Implement the `Cashier` class: | ||
|
||
* `Cashier(int n, int discount, int[] products, int[] prices)` Initializes the object with `n`, the `discount`, the `products` and their `prices`. | ||
* `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. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input | ||
["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]]] | ||
Output | ||
[null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0] | ||
Explanation | ||
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]); // return 500.0, bill = 1 * 100 + 2 * 200 = 500. | ||
cashier.getBill([3,7],[10,10]); // return 4000.0 | ||
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. | ||
cashier.getBill([4],[10]); // return 4000.0 | ||
cashier.getBill([7,3],[10,10]); // return 4000.0 | ||
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 | ||
cashier.getBill([2,3,5],[5,3,2]); // return 2500.0 | ||
``` | ||
|
||
Constraints: | ||
|
||
* `1 <= n <= 10^4` | ||
* `0 <= discount <= 100` | ||
* `1 <= products.length <= 200` | ||
* `1 <= products[i] <= 200` | ||
* There are not repeated elements in the array `products`. | ||
* `prices.length == products.length` | ||
* `1 <= prices[i] <= 1000` | ||
* `1 <= product.length <= products.length` | ||
* `product[i]` exists in `products.` | ||
* `amount.length == product.length` | ||
* `1 <= amount[i] <= 1000` | ||
* At most `1000` calls will be made to `getBill`. | ||
* Answers within `10^-5` of the actual value will be accepted as correct. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @param {number} n | ||
* @param {number} discount | ||
* @param {number[]} products | ||
* @param {number[]} prices | ||
*/ | ||
var Cashier = function (n, discount, products, prices) { | ||
this.n = n | ||
this.discount = discount | ||
|
||
this.productPriceMap = {} | ||
for (let i = 0; i < products.length; i++) { | ||
this.productPriceMap[products[i]] = prices[i] | ||
} | ||
|
||
this.current = 0 | ||
} | ||
|
||
/** | ||
* @param {number[]} product | ||
* @param {number[]} amount | ||
* @return {number} | ||
*/ | ||
Cashier.prototype.getBill = function (product, amount) { | ||
this.current++ | ||
|
||
const totalBeforeDiscount = product.reduce((acc, cur, idx) => { | ||
const cost = this.productPriceMap[cur] * amount[idx] | ||
|
||
return acc + cost | ||
}, 0) | ||
|
||
const bill = (this.current % this.n === 0) | ||
? totalBeforeDiscount - (this.discount / 100 * totalBeforeDiscount) | ||
: totalBeforeDiscount | ||
|
||
return bill | ||
} | ||
|
||
/** | ||
* Your Cashier object will be instantiated and called as such: | ||
* var obj = new Cashier(n, discount, products, prices) | ||
* var param_1 = obj.getBill(product,amount) | ||
*/ | ||
|
||
module.exports = Cashier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const Cashier = require('./solution') | ||
|
||
test('Example 1', () => { | ||
const cashier = new Cashier(3, 50, [1, 2, 3, 4, 5, 6, 7], [100, 200, 300, 400, 300, 200, 100]) | ||
|
||
const bill1 = cashier.getBill([1, 2], [1, 2]) // return 500.0, bill = 1 * 100 + 2 * 200 = 500. | ||
expect(bill1).toBe(500) | ||
|
||
const bill2 = cashier.getBill([3, 7], [10, 10]) // return 4000.0 | ||
expect(bill2).toBe(4000) | ||
|
||
const bill3 = 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. | ||
expect(bill3).toBe(800) | ||
|
||
const bill4 = cashier.getBill([4], [10]) // return 4000.0 | ||
expect(bill4).toBe(4000) | ||
|
||
const bill5 = cashier.getBill([7, 3], [10, 10]) // return 4000.0 | ||
expect(bill5).toBe(4000) | ||
|
||
const bill6 = 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 | ||
expect(bill6).toBe(7350) | ||
|
||
const bill7 = cashier.getBill([2, 3, 5], [5, 3, 2]) // return 2500.0 | ||
expect(bill7).toBe(2500) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Closest Divisors | ||
|
||
LeetCode #: [1362](https://leetcode.com/problems/closest-divisors/) | ||
|
||
Difficulty: Medium | ||
|
||
Topics: Math. | ||
|
||
## Problem | ||
|
||
Given an integer `num`, find the closest two integers in absolute difference whose product equals `num + 1` or `num + 2`. | ||
|
||
Return the two integers in any order. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: num = 8 | ||
Output: [3,3] | ||
Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: num = 123 | ||
Output: [5,25] | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: num = 999 | ||
Output: [40,25] | ||
``` | ||
|
||
Constraints: | ||
|
||
* `1 <= num <= 10^9` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const getDivisor = (num) => { | ||
const sqrtfloor = Math.floor(Math.sqrt(num)) | ||
|
||
for (let i = sqrtfloor; i >= 1; i--) { | ||
if (num % i === 0) { | ||
return [i, num / i] | ||
} | ||
} | ||
} | ||
|
||
const closestDivisors = (num) => { | ||
const divisor1 = getDivisor(num + 1) | ||
const divisor2 = getDivisor(num + 2) | ||
|
||
const diff1 = divisor1[1] - divisor1[0] | ||
const diff2 = divisor2[1] - divisor2[0] | ||
|
||
return (diff1 < diff2) | ||
? divisor1 | ||
: divisor2 | ||
} | ||
|
||
module.exports = closestDivisors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const closestDivisors = require('./closestDivisors') | ||
|
||
test('Example 1', () => { | ||
const num = 8 | ||
|
||
const result = closestDivisors(num) | ||
|
||
expect(result).toStrictEqual([3, 3]) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const num = 123 | ||
|
||
const result = closestDivisors(num) | ||
|
||
expect(result).toStrictEqual([5, 25]) | ||
}) | ||
|
||
test('Example 3', () => { | ||
const num = 999 | ||
|
||
const result = closestDivisors(num) | ||
|
||
expect(result).toStrictEqual([25, 40]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Number of Days Between Two Dates | ||
|
||
LeetCode #: [1360](https://leetcode.com/problems/number-of-days-between-two-dates/) | ||
|
||
Difficulty: Easy | ||
|
||
Topics: - | ||
|
||
## Problem | ||
|
||
Write a program to count the number of days between two dates. | ||
|
||
The two dates are given as strings, their format is `YYYY-MM-DD` as shown in the examples. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: date1 = "2019-06-29", date2 = "2019-06-30" | ||
Output: 1 | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: date1 = "2020-01-15", date2 = "2019-12-31" | ||
Output: 15 | ||
``` | ||
|
||
Constraints: | ||
|
||
* The given dates are valid dates between the years `1971` and `2100`. |
12 changes: 12 additions & 0 deletions
12
problems/number-of-days-between-two-dates/daysBetweenDates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const oneDay = 86400000 | ||
|
||
const daysBetweenDates = (date1, date2) => { | ||
const d1 = new Date(date1) | ||
const d2 = new Date(date2) | ||
|
||
const diff = Math.abs(d1 - d2) / oneDay | ||
|
||
return diff | ||
} | ||
|
||
module.exports = daysBetweenDates |
19 changes: 19 additions & 0 deletions
19
problems/number-of-days-between-two-dates/daysBetweenDates.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const daysBetweenDates = require('./daysBetweenDates') | ||
|
||
test('Example 1', () => { | ||
const date1 = '2019-06-29' | ||
const date2 = '2019-06-30' | ||
|
||
const result = daysBetweenDates(date1, date2) | ||
|
||
expect(result).toBe(1) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const date1 = '2020-01-15' | ||
const date2 = '2019-12-31' | ||
|
||
const result = daysBetweenDates(date1, date2) | ||
|
||
expect(result).toBe(15) | ||
}) |
41 changes: 41 additions & 0 deletions
41
problems/number-of-substrings-containing-all-three-characters/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Number of Substrings Containing All Three Characters | ||
|
||
LeetCode #: [1358](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/) | ||
|
||
Difficulty: Medium | ||
|
||
Topics: String. | ||
|
||
## Problem | ||
|
||
Given a string `s` consisting only of characters *a*, *b* and *c*. | ||
|
||
Return the number of substrings containing at least one occurrence of all these characters *a*, *b* and *c*. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: s = "abcabc" | ||
Output: 10 | ||
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: s = "aaacb" | ||
Output: 3 | ||
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb". | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: s = "abc" | ||
Output: 1 | ||
``` | ||
|
||
Constraints: | ||
|
||
* `3 <= s.length <= 5 x 10^4` | ||
* `s` only consists of *a*, *b* or *c* characters. |
Oops, something went wrong.