-
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
18 changed files
with
634 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,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` |
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,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 |
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,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) | ||
}) |
46 changes: 46 additions & 0 deletions
46
problems/minimum-deletion-cost-to-avoid-repeating-letters/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,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. |
27 changes: 27 additions & 0 deletions
27
problems/minimum-deletion-cost-to-avoid-repeating-letters/minCost.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,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 |
37 changes: 37 additions & 0 deletions
37
problems/minimum-deletion-cost-to-avoid-repeating-letters/minCost.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,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) | ||
}) |
55 changes: 55 additions & 0 deletions
55
...ber-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/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,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` |
40 changes: 40 additions & 0 deletions
40
...s/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/numTriplets.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,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 |
37 changes: 37 additions & 0 deletions
37
...ber-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/numTriplets.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,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) | ||
}) |
Oops, something went wrong.