Skip to content

Commit

Permalink
Merge branch 'release/1.29.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Sep 6, 2020
2 parents cae839b + 922bf25 commit b8be9d7
Show file tree
Hide file tree
Showing 18 changed files with 634 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](/problems/average-salary-excluding-the-minimum-and-maximum-salary) | Easy | Array, Sort |
| 1492 | [The kth Factor of n](/problems/the-kth-factor-of-n) | Medium | Math |
| 1493 | [Longest Subarray of 1's After Deleting One Element](/problems/longest-subarray-of-1s-after-deleting-one-element) | Medium | Array |
| 1572 | [Matrix Diagonal Sum](/problems/matrix-diagonal-sum) | Easy | Array |
| 1574 | [Shortest Subarray to be Removed to Make Array Sorted](/problems/shortest-subarray-to-be-removed-to-make-array-sorted) | Medium | Array, Binary Search |
| 1576 | [Replace All ?'s to Avoid Consecutive Repeating Characters](/problems/replace-all-s-to-avoid-consecutive-repeating-characters) | Easy | String |
| 1577 | [Number of Ways Where Square of Number Is Equal to Product of Two Numbers](/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers) | Medium | Hash Table, Math |
| 1578 | [Minimum Deletion Cost to Avoid Repeating Letters](/problems/minimum-deletion-cost-to-avoid-repeating-letters) | Medium | Greedy |

## Questions / Issues

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.28.1",
"version": "1.29.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down
49 changes: 49 additions & 0 deletions problems/matrix-diagonal-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Matrix Diagonal Sum

LeetCode #: [1572](https://leetcode.com/problems/matrix-diagonal-sum/)

Difficulty: Easy

Topics: Array.

## Problem

Given a square matrix `mat`, return the sum of the matrix diagonals.

Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

Example 1:

![Example 1](https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png)

```text
Input: mat = [[1,2,3],
[4,5,6],
[7,8,9]]
Output: 25
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Notice that element mat[1][1] = 5 is counted only once.
```

Example 2:

```text
Input: mat = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]]
Output: 8
```

Example 3:

```text
Input: mat = [[5]]
Output: 5
```

Constraints:

- `n == mat.length == mat[i].length`
- `1 <= n <= 100`
- `1 <= mat[i][j] <= 100`
21 changes: 21 additions & 0 deletions problems/matrix-diagonal-sum/diagonalSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[][]} mat
* @return {number}
*/
const diagonalSum = (mat) => {
const length = mat.length
let sum = 0

for (let i = 0; i < length; i++) {
sum += mat[i][i] + mat[i][length - 1 - i]
}

if (length % 2 === 1) {
const middleIndex = (length - 1) / 2
sum -= mat[middleIndex][middleIndex]
}

return sum
}

module.exports = diagonalSum
34 changes: 34 additions & 0 deletions problems/matrix-diagonal-sum/diagonalSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const diagonalSum = require('./diagonalSum')

test('Example 1', () => {
const mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

const result = diagonalSum(mat)

expect(result).toBe(25)
})

test('Example 2', () => {
const mat = [
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]
]

const result = diagonalSum(mat)

expect(result).toBe(8)
})

test('Example 3', () => {
const mat = [[5]]

const result = diagonalSum(mat)

expect(result).toBe(5)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Minimum Deletion Cost to Avoid Repeating Letters

LeetCode #: [1578](https://leetcode.com/problems/minimum-deletion-cost-to-avoid-repeating-letters/)

Difficulty: Medium

Topics: Greedy.

## Problem

Given a string `s` and an array of integers `cost` where `cost[i]` is the cost of deleting the character `i` in `s`.

Return the minimum cost of deletions such that there are no two identical letters next to each other.

Notice that you will delete the chosen characters at the same time, in other words, after deleting a character, the costs of deleting other characters will not change.

Example 1:

```text
Input: s = "abaac", cost = [1,2,3,4,5]
Output: 3
Explanation: Delete the letter "a" with cost 3 to get "abac" (String without two identical letters next to each other).
```

Example 2:

```text
Input: s = "abc", cost = [1,2,3]
Output: 0
Explanation: You don't need to delete any character because there are no identical letters next to each other.
```

Example 3:

```text
Input: s = "aabaa", cost = [1,2,3,4,1]
Output: 2
Explanation: Delete the first and the last character, getting the string ("aba").
```

Constraints:

- `s.length == cost.length`
- `1 <= s.length, cost.length <= 10^5`
- `1 <= cost[i] <= 10^4`
- `s` contains only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {string} s
* @param {number[]} cost
* @return {number}
*/
const minCost = function (s, cost) {
let result = 0
let max = cost[0]
let sum = cost[0]

for (let i = 1; i < s.length; i++) {
if (s[i] !== s[i - 1]) {
result += sum - max
sum = cost[i]
max = cost[i]
} else {
sum += cost[i]
max = Math.max(max, cost[i])
}
}

result += sum - max

return result
}

module.exports = minCost
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const minCost = require('./minCost')

test('Example 1', () => {
const s = 'abaac'
const cost = [1, 2, 3, 4, 5]

const result = minCost(s, cost)

expect(result).toBe(3)
})

test('Example 2', () => {
const s = 'abc'
const cost = [1, 2, 3]

const result = minCost(s, cost)

expect(result).toBe(0)
})

test('Example 3', () => {
const s = 'aabaa'
const cost = [1, 2, 3, 4, 1]

const result = minCost(s, cost)

expect(result).toBe(2)
})

test('caaac', () => {
const s = 'caaac'
const cost = [0, 2, 4, 3, 0]

const result = minCost(s, cost)

expect(result).toBe(5)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Number of Ways Where Square of Number Is Equal to Product of Two Numbers

LeetCode #: [1577](https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/)

Difficulty: Medium

Topics: Hash Table, Math.

## Problem

Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:

- Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
- Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j] * nums1[k]` where `0 <= i < nums2.length` and `0 <= j < k < nums1.length`.

Example 1:

```text
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
```

Example 2:

```text
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1[i]^2 = nums2[j] * nums2[k].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
```

Example 3:

```text
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
```

Example 4:

```text
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
```

Constraints:

- `1 <= nums1.length, nums2.length <= 1000`
- `1 <= nums1[i], nums2[i] <= 10^5`
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const sortBy = require('lodash/sortBy')
const sortedIndexOf = require('lodash/sortedIndexOf')
const sortedLastIndexOf = require('lodash/sortedLastIndexOf')

const getNum = (sorted1, sorted2) => {
let result = 0

for (const num1 of sorted1) {
const squared = num1 * num1

for (let aIndex = 0; aIndex < sorted2.length; aIndex++) {
const a = sorted2[aIndex]
const b = squared / a

const bIndex = sortedIndexOf(sorted2, b)
const bLastIndex = sortedLastIndexOf(sorted2, b)

if (aIndex <= bLastIndex) {
const count = (bLastIndex - Math.max(aIndex + 1, bIndex) + 1)
result += count
}
}
}

return result
}

/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
const numTriplets = function (nums1, nums2) {
const sorted1 = sortBy(nums1)
const sorted2 = sortBy(nums2)

return getNum(sorted1, sorted2) + getNum(sorted2, sorted1)
}

module.exports = numTriplets
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const numTriplets = require('./numTriplets')

test('Example 1', () => {
const nums1 = [7, 4]
const nums2 = [5, 2, 8, 9]

const result = numTriplets(nums1, nums2)

expect(result).toBe(1)
})

test('Example 2', () => {
const nums1 = [1, 1]
const nums2 = [1, 1, 1]

const result = numTriplets(nums1, nums2)

expect(result).toBe(9)
})

test('Example 3', () => {
const nums1 = [7, 7, 8, 3]
const nums2 = [1, 2, 9, 7]

const result = numTriplets(nums1, nums2)

expect(result).toBe(2)
})

test('Example 4', () => {
const nums1 = [4, 7, 9, 11, 23]
const nums2 = [3, 5, 1024, 12, 18]

const result = numTriplets(nums1, nums2)

expect(result).toBe(0)
})
Loading

0 comments on commit b8be9d7

Please sign in to comment.