-
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
6 changed files
with
106 additions
and
5 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
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. |
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,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) | ||
``` |
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,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 |
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,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) | ||
}) |
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