Skip to content

Commit bf25304

Browse files
committed
Merge branch 'release/1.2.0'
2 parents d66f0be + e533d05 commit bf25304

File tree

6 files changed

+106
-5
lines changed

6 files changed

+106
-5
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
# leetcode
2+
23
My solutions for [LeetCode problems](https://leetcode.com/problemset/all/).
34

45
## Usage
56

7+
### Running and testing locally in your machine
8+
69
First, install dependencies:
710

8-
```
11+
```shell
912
npm install
1013
```
1114

1215
To run tests with jest:
1316

14-
```
17+
```shell
1518
npm run test
1619
```
1720

21+
### Submitting / Running in LeetCode
22+
23+
1. Find your problem in the [problems](/problems) folder.
24+
2. Copy the solution.
25+
3. Paste the code into LeetCode.
26+
4. Hit the Run or Submit button.
27+
1828
## Solutions
1929

2030
| LeetCode # | Title | Difficulty | Topics |
2131
|-----------:|:------|:-----------|:-------|
2232
| 1 | [Two Sum](/problems/two-sum) | Easy | Array, hash table |
2333
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
34+
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash Table, two pointers, binary search, sort, set |
35+
36+
## Questions / Issues
37+
38+
If you have any question, feel free to open a GitHub issue and reach out to me.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leetcode",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "My solutions for LeetCode problems.",
55
"main": "index.js",
66
"scripts": {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Intersection of Two Arrays
2+
3+
[Link to LeetCode page](https://leetcode.com/problems/intersection-of-two-arrays/)
4+
5+
Difficulty: Easy
6+
7+
Topics: Hash table, two pointers, binary search, sort, set.
8+
9+
## Solution Explanation
10+
11+
We convert the arrays into two [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) objects - short set and long set. This would remove duplicate values in each array.
12+
13+
Then we iterate through the short set and check if the value exists in the long set. Since it is a set object, the check performs in constant O(1) time complexity.
14+
15+
## Complexity Analysis
16+
17+
Assume m is the length of nums1 and n is the length of nums2.
18+
19+
Time complexity: O(m + n) because we go through every elements in both arrays once during the set creation.
20+
21+
Space complexity: O(m + n) because in worst case scenario we create two sets of length m and n when both arrays contain totally unique values.
22+
23+
## Tests
24+
25+
In the problem description on LeetCode page, it is noted that:
26+
27+
- Each element in the result must be unique.
28+
- The result can be in any order.
29+
30+
Because of this, the assertion in our tests has to be in the following format:
31+
32+
```javascript
33+
expect(result).toHaveLength(2)
34+
expect(result).toContain(9)
35+
expect(result).toContain(4)
36+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const toSets = (nums1, nums2) => {
2+
if (nums1.length <= nums2.length) {
3+
return {
4+
shortSet: new Set(nums1),
5+
longSet: new Set(nums2)
6+
}
7+
}
8+
9+
return {
10+
shortSet: new Set(nums2),
11+
longSet: new Set(nums1)
12+
}
13+
}
14+
15+
const intersection = (nums1, nums2) => {
16+
const { shortSet, longSet } = toSets(nums1, nums2)
17+
18+
const result = []
19+
for (const num of shortSet) {
20+
if (longSet.has(num)) {
21+
result.push(num)
22+
}
23+
}
24+
25+
return result
26+
}
27+
28+
module.exports = intersection
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const intersection = require('./solution')
2+
3+
test('Sample 1', () => {
4+
const nums1 = [1, 2, 2, 1]
5+
const nums2 = [2, 2]
6+
7+
const result = intersection(nums1, nums2)
8+
9+
expect(result).toHaveLength(1)
10+
expect(result).toContain(2)
11+
})
12+
13+
test('Sample 2', () => {
14+
const nums1 = [4, 9, 5]
15+
const nums2 = [9, 4, 9, 8, 4]
16+
17+
const result = intersection(nums1, nums2)
18+
19+
expect(result).toHaveLength(2)
20+
expect(result).toContain(9)
21+
expect(result).toContain(4)
22+
})

problems/single-number/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Two Sum
1+
# Single Number
22

3-
[Link to LeetCode page](https://leetcode.com/problems/two-sum/)
3+
[Link to LeetCode page](https://leetcode.com/problems/single-number/)
44

55
Difficulty: Easy
66

0 commit comments

Comments
 (0)