Skip to content

Commit

Permalink
Merge branch 'release/1.18.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Feb 29, 2020
2 parents e7b7b1e + 3404b36 commit d847a9f
Show file tree
Hide file tree
Showing 21 changed files with 4,079 additions and 2,462 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ Folder structure in this repository: https://<span></span>github.com/ecgan/leetc
| 1346 | [Check If N and Its Double Exist](/problems/check-if-n-and-its-double-exist) | Easy | Array |
| 1347 | [Minimum Number of Steps to Make Two Strings Anagram](/problems/minimum-number-of-steps-to-make-two-strings-anagram) | Medium | String |
| 1348 | [Tweet Counts Per Frequency](/problems/tweet-counts-per-frequency) | Medium | Design |
| 1356 | [Sort Integers by The Number of 1 Bits](/problems/sort-integers-by-the-number-of-1-bits) | Easy | Sort, bit manipulation |
| 1357 | [Apply Discount Every n Orders](/problems/apply-discount-every-n-orders) | Medium | Design |
| 1358 | [Number of Substrings Containing All Three Characters](/problems/number-of-substrings-containing-all-three-characters) | Medium | String |
| 1360 | [Number of Days Between Two Dates](/problems/number-of-days-between-two-dates) | Easy | - |
| 1361 | [Validate Binary Tree Nodes](/problems/validate-binary-tree-nodes) | Medium | Graph |
| 1362 | [Closest Divisors](/problems/closest-divisors) | Medium | Math |

## Questions / Issues

Expand Down
5,777 changes: 3,325 additions & 2,452 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.17.0",
"version": "1.18.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand All @@ -22,16 +22,16 @@
},
"homepage": "https://github.com/ecgan/leetcode",
"devDependencies": {
"codecov": "^3.5.0",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^22.14.0",
"eslint-plugin-node": "^9.1.0",
"codecov": "^3.6.5",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jest": "^23.8.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.8.0",
"leettree": "^0.2.0"
"eslint-plugin-standard": "^4.0.1",
"jest": "^25.1.0",
"leettree": "^0.3.0"
},
"dependencies": {
"lodash": "^4.17.15"
Expand Down
57 changes: 57 additions & 0 deletions problems/apply-discount-every-n-orders/README.md
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.
46 changes: 46 additions & 0 deletions problems/apply-discount-every-n-orders/solution.js
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
26 changes: 26 additions & 0 deletions problems/apply-discount-every-n-orders/solution.test.js
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)
})
39 changes: 39 additions & 0 deletions problems/closest-divisors/README.md
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`
23 changes: 23 additions & 0 deletions problems/closest-divisors/closestDivisors.js
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
25 changes: 25 additions & 0 deletions problems/closest-divisors/closestDivisors.test.js
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])
})
31 changes: 31 additions & 0 deletions problems/number-of-days-between-two-dates/README.md
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 problems/number-of-days-between-two-dates/daysBetweenDates.js
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 problems/number-of-days-between-two-dates/daysBetweenDates.test.js
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)
})
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.
Loading

0 comments on commit d847a9f

Please sign in to comment.