Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Aug 1, 2019
2 parents d66f0be + e533d05 commit bf25304
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 5 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
# leetcode

My solutions for [LeetCode problems](https://leetcode.com/problemset/all/).

## Usage

### Running and testing locally in your machine

First, install dependencies:

```
```shell
npm install
```

To run tests with jest:

```
```shell
npm run test
```

### Submitting / Running in LeetCode

1. Find your problem in the [problems](/problems) folder.
2. Copy the solution.
3. Paste the code into LeetCode.
4. Hit the Run or Submit button.

## Solutions

| LeetCode # | Title | Difficulty | Topics |
|-----------:|:------|:-----------|:-------|
| 1 | [Two Sum](/problems/two-sum) | Easy | Array, hash table |
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash Table, two pointers, binary search, sort, set |

## Questions / Issues

If you have any question, feel free to open a GitHub issue and reach out to me.
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.1.0",
"version": "1.2.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down
36 changes: 36 additions & 0 deletions problems/intersection-of-two-arrays/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Intersection of Two Arrays

[Link to LeetCode page](https://leetcode.com/problems/intersection-of-two-arrays/)

Difficulty: Easy

Topics: Hash table, two pointers, binary search, sort, set.

## Solution Explanation

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.

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.

## Complexity Analysis

Assume m is the length of nums1 and n is the length of nums2.

Time complexity: O(m + n) because we go through every elements in both arrays once during the set creation.

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.

## Tests

In the problem description on LeetCode page, it is noted that:

- Each element in the result must be unique.
- The result can be in any order.

Because of this, the assertion in our tests has to be in the following format:

```javascript
expect(result).toHaveLength(2)
expect(result).toContain(9)
expect(result).toContain(4)
```
28 changes: 28 additions & 0 deletions problems/intersection-of-two-arrays/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const toSets = (nums1, nums2) => {
if (nums1.length <= nums2.length) {
return {
shortSet: new Set(nums1),
longSet: new Set(nums2)
}
}

return {
shortSet: new Set(nums2),
longSet: new Set(nums1)
}
}

const intersection = (nums1, nums2) => {
const { shortSet, longSet } = toSets(nums1, nums2)

const result = []
for (const num of shortSet) {
if (longSet.has(num)) {
result.push(num)
}
}

return result
}

module.exports = intersection
22 changes: 22 additions & 0 deletions problems/intersection-of-two-arrays/solution.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const intersection = require('./solution')

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

const result = intersection(nums1, nums2)

expect(result).toHaveLength(1)
expect(result).toContain(2)
})

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

const result = intersection(nums1, nums2)

expect(result).toHaveLength(2)
expect(result).toContain(9)
expect(result).toContain(4)
})
4 changes: 2 additions & 2 deletions problems/single-number/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Two Sum
# Single Number

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

Difficulty: Easy

Expand Down

0 comments on commit bf25304

Please sign in to comment.