Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e17577

Browse files
Merge pull request #64 from amejiarosario/improvements
Improvements
2 parents 9874647 + a93ac1b commit 7e17577

File tree

17 files changed

+480
-14
lines changed

17 files changed

+480
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// brute force: O(nd) | O(1)
2+
function rotLeft(a, d) {
3+
for (let i = 0; i < d; i++) { // O(d)
4+
a.push(a.shift()); // O(n), shift O(n)
5+
}
6+
return a;
7+
}
8+
9+
module.exports = rotLeft;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// modulus for rotations: O(n^2) | O(1)
2+
function rotLeft(a, d) {
3+
const len = a.length;
4+
const rot = d % len;
5+
for (let i = 0; i < rot; i++) { // O(n^2)
6+
a.push(a.shift()); // O(n)
7+
}
8+
return a;
9+
}
10+
11+
module.exports = rotLeft;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// additional space: O(n) | O(n)
2+
function rotLeft(a, d) {
3+
const len = a.length;
4+
const rot = d % len;
5+
const b = [];
6+
for (let i = 0; i < len; i++) { // O(n)
7+
b[i] = a[(rot + i) % len]; // O(1)
8+
}
9+
return b;
10+
}
11+
12+
module.exports = rotLeft;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*eslint-disable */
2+
// npx jest benchmarks/two-sum-implementations/two-sum.spec.js --watch --collectCoverage
3+
const implementations = [
4+
{ name: 1, fn: require('./01-array-rotation') },
5+
{ name: '1a', fn: require('./01a-array-rotation') },
6+
{ name: 2, fn: require('./02-array-rotation') },
7+
];
8+
9+
implementations.forEach(({name, fn}) => {
10+
describe(`Two Sum: ${name}`, () => {
11+
it('should work on worst case', () => {
12+
const rots = 1000;
13+
const array = [1, 2, 3];
14+
expect(fn(array, rots)).toEqual([2,3,1]);
15+
});
16+
});
17+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Brute force: O(n^2) | O(1)
2+
function twoSum(nums, target) {
3+
for (let i = 0; i < nums.length - 1; i++) { // O(n^2)
4+
const diff = target - nums[i];
5+
const offset = i + 1;
6+
const idx = nums.slice(offset).findIndex((n) => n === diff); // O(n)
7+
const j = offset + idx;
8+
if (idx > -1) return [i, j];
9+
}
10+
return [];
11+
}
12+
13+
module.exports = twoSum;

‎benchmarks/two-sum-implementations/02-two-sum.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// With a HashMap: O(n) | O(n)
1+
// [map w/dups handling] → O(n^2) | O(n)
22
function twoSum(nums, target) {
33
const map = nums.reduce((m, v, i) => { // O(n)
44
const ids = m.get(v) || [];
@@ -8,8 +8,9 @@ function twoSum(nums, target) {
88

99
for (let i = 0; i < nums.length; i++) { // O(n)
1010
const diff = target - nums[i];
11-
if (map.has(diff) && i !== map.get(diff)) {
12-
return [i, map.get(diff)];
11+
if (map.has(diff)) {
12+
const id = map.get(diff).find((j) => j > i);
13+
if (id > -1) return [i, id];
1314
}
1415
}
1516

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// [map w/dups handling] → O(n^2) | O(n)
2+
function twoSum(nums, target) {
3+
const map = mapify(nums);
4+
5+
for (let i = 0; i < nums.length; i++) { // O(n)
6+
const diff = target - nums[i];
7+
if (map.has(diff)) {
8+
const id = map.get(diff).find((j) => j > i);
9+
if (id > -1) return [i, id];
10+
}
11+
}
12+
13+
return [];
14+
}
15+
16+
function mapify(nums) {
17+
return nums.reduce((m, v, i) => { // O(n)
18+
const ids = m.get(v) || [];
19+
ids.push(i);
20+
return m.set(v, ids);
21+
}, new Map());
22+
}
23+
24+
module.exports = twoSum;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// sort + two pointers: O(n log n) | O(1)
2+
function twoSum(nums, target) {
3+
nums.sort((a, b) => a - b);
4+
5+
let lo = 0;
6+
let hi = nums.length - 1;
7+
8+
while (lo < hi) {
9+
const sum = nums[lo] + nums[hi];
10+
if (sum === target) {
11+
return [lo, hi];
12+
}
13+
14+
if (sum > target) {
15+
hi--;
16+
} else {
17+
lo++;
18+
}
19+
}
20+
return [];
21+
}
22+
23+
module.exports = twoSum;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*eslint-disable */
2+
// npx jest benchmarks/two-sum-implementations/two-sum.spec.js --watch --collectCoverage
3+
const implementations = [
4+
{ name: 1, fn: require('./01-two-sum') },
5+
{ name: '1a', fn: require('./01a-two-sum') },
6+
{ name: 2, fn: require('./02-two-sum') },
7+
{ name: '2a', fn: require('./02a-two-sum') },
8+
{ name: 3, fn: require('./03-two-sum') },
9+
{ name: 4, fn: require('./04-two-sum') },
10+
];
11+
12+
implementations.forEach(({name, fn}) => {
13+
describe(`Two Sum: ${name}`, () => {
14+
xit('should work', () => {
15+
expect(fn([1, 2, 3].concat(Array(1e2 - 3).fill(7)), 4)).toEqual([0, 2]);
16+
});
17+
18+
it('should work on worst case', () => {
19+
const size = 100;
20+
expect(fn([...Array(size).fill(2), 3, 3 * size * 10], 3 * size * 10 + 3)).toEqual([size, size + 1]);
21+
});
22+
});
23+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const checkIfPrerequisite = function (n, prerequisites, queries) {
2+
const graph = new Map();
3+
4+
Array(n).fill(0).forEach((v, i) => graph.set(i, {
5+
children: [],
6+
// connected: new Set(),
7+
}));
8+
9+
prerequisites.forEach(([u, v]) => {
10+
graph.get(u).children.push(v);
11+
// graph.get(u).connected.add(v);
12+
});
13+
14+
15+
return queries.map(([u, v]) => isConnected(graph, u, v, new Set(), u));
16+
};
17+
18+
function isConnected(graph, u, v, path = new Set(), p) {
19+
// console.log({u, v, path}, graph)
20+
// if (graph.get(u).connected.has(v)) return true;
21+
// path.forEach(s => graph.get(p).connected.add(s));
22+
23+
for (const child of graph.get(u).children) {
24+
if (child === v) return true;
25+
if (path.has(child)) continue;
26+
if (isConnected(graph, child, v, path.add(u), p)) return true;
27+
}
28+
29+
return false;
30+
}
31+
32+
33+
module.exports = checkIfPrerequisite;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /