Skip to content

Commit 316e453

Browse files
committed
update
1 parent 95fa562 commit 316e453

21 files changed

+366
-1
lines changed

README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![Language](https://img.shields.io/badge/language-Python%20%2F%20Modern%20C++-orange.svg) 
44
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md) 
55
![Update](https://img.shields.io/badge/update-weekly-green.svg) 
6-
![Progress](https://img.shields.io/badge/progress-2617%20%2F%202617-ff69b4.svg) 
6+
![Progress](https://img.shields.io/badge/progress-2638%20%2F%202638-ff69b4.svg) 
77
[![SayThanks](https://img.shields.io/badge/say-thanks-ff69f4.svg)](https://saythanks.io/to/kamyu104) 
88
![Visitors](https://visitor-badge.laobi.icu/badge?page_id=kamyu104.leetcode.solutions)
99

@@ -46,6 +46,10 @@
4646
* [Design](https://github.com/kamyu104/LeetCode-Solutions#design)
4747
* [Concurrency](https://github.com/kamyu104/LeetCode-Solutions#concurrency)
4848

49+
## JavaScript
50+
51+
* [JavaScript](https://github.com/kamyu104/LeetCode-Solutions#javascript)
52+
4953
## Database
5054

5155
* [SQL](https://github.com/kamyu104/LeetCode-Solutions#sql)
@@ -1754,6 +1758,36 @@
17541758
</div>
17551759
<br/>
17561760

1761+
## JavaScript
1762+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
1763+
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
1764+
2618 | [Check if Object Instance of Class](https://leetcode.com/problems/check-if-object-instance-of-class/) | [TypeScript](./TypeScript/check-if-object-instance-of-class.ts) | _O(n)_ | _O(1)_ | Medium | | |
1765+
2619 | [Array Prototype Last](https://leetcode.com/problems/array-prototype-last/) | [TypeScript](./TypeScript/array-prototype-last.ts) | _O(1)_ | _O(1)_ | Easy | | |
1766+
2620 | [Counter](https://leetcode.com/problems/counter/) | [TypeScript](./TypeScript/counter.ts) | _O(1)_ | _O(1)_ | Easy | | |
1767+
2621 | [Sleep](https://leetcode.com/problems/sleep/) | [TypeScript](./TypeScript/sleep.ts) | _O(1)_ | _O(1)_ | Easy | | |
1768+
2622 | [Cache With Time Limit](https://leetcode.com/problems/cache-with-time-limit/) | [TypeScript](./TypeScript/cache-with-time-limit.ts) | _O(1)_ | _O(1)_ | Easy | | |
1769+
2623 | [Memoize](https://leetcode.com/problems/memoize/) | [TypeScript](./TypeScript/memoize.ts) | _O(1)_ | _O(1)_ | Easy | | Hash Tablle |
1770+
2624 | [Snail traversal](https://leetcode.com/problems/snail-traversal/) | [TypeScript](./TypeScript/snail-traversal.ts) | _O(m * n)_ | _O(1)_ | Medium | | |
1771+
2625 | [Flatten Deeply Nested Array](https://leetcode.com/problems/flatten-deeply-nested-array/) | [TypeScript](./TypeScript/flatten-deeply-nested-array.ts) | _O(n)_ | _O(h)_ | Medium | | |
1772+
2626 | [Array Reduce Transformation](https://leetcode.com/problems/array-reduce-transformation/) | [TypeScript](./TypeScript/array-reduce-transformation.ts) | _O(n)_ | _O(1)_ | Easy | | |
1773+
2627 | [Debounce](https://leetcode.com/problems/debounce/) | [TypeScript](./TypeScript/debounce.ts) | _O(1)_ | _O(1)_ | Medium | | |
1774+
2628 | [JSON Deep Equal](https://leetcode.com/problems/json-deep-equal/) | [TypeScript](./TypeScript/json-deep-equal.ts) | _O(n)_ | _O(h)_ | Medium | | DFS |
1775+
2629 | [Function Composition](https://leetcode.com/problems/function-composition/) | [TypeScript](./TypeScript/function-composition.ts) | _O(n)_ | _O(1)_ | Easy | | |
1776+
2630 | [Memoize II](https://leetcode.com/problems/memoize-ii/) | [TypeScript](./TypeScript/memoize-ii.ts) | _O(n)_ | _O(t)_ | Hard | | Trie |
1777+
2631 | [Group By](https://leetcode.com/problems/group-by/) | [TypeScript](./TypeScript/group-by.ts) | _O(n)_ | _O(n)_ | Medium | | |
1778+
2632 | [Curry](https://leetcode.com/problems/curry/) | [TypeScript](./TypeScript/curry.ts) | _O(n)_ | _O(n)_ | Medium | | |
1779+
2633 | [Convert Object to JSON String](https://leetcode.com/problems/convert-object-to-json-string/) | [TypeScript](./TypeScript/convert-object-to-json-string.ts) | _O(n)_ | _O(n)_ | Medium | | DFS |
1780+
2634 | [Filter Elements from Array](https://leetcode.com/problems/filter-elements-from-array/) | [TypeScript](./TypeScript/filter-elements-from-array.ts) | _O(n)_ | _O(1)_ | Easy | | |
1781+
2635 | [Apply Transform Over Each Element in Array](https://leetcode.com/problems/apply-transform-over-each-element-in-array/) | [TypeScript](./TypeScript/apply-transform-over-each-element-in-array.ts) | _O(n)_ | _O(1)_ | Easy | | |
1782+
2636 | [Promise Pool](https://leetcode.com/problems/apply-transform-over-each-element-in-array/) | [TypeScript](./TypeScript/apply-transform-over-each-element-in-array.ts) | _O(n / c)_ | _O(c)_ | Easy | | |
1783+
2637 | [Promise Time Limit](https://leetcode.com/problems/promise-time-limit/) | [TypeScript](./TypeScript/promise-time-limit.ts) | _O(n)_ | _O(1)_ | Medium | | |
1784+
1785+
<br/>
1786+
<div align="right">
1787+
<b><a href="#algorithms">⬆️ Back to Top</a></b>
1788+
</div>
1789+
<br/>
1790+
17571791
## SQL
17581792
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
17591793
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
5+
let result = [];
6+
arr.forEach((x, i) => {
7+
result.push(fn(x, i));
8+
});
9+
return result;
10+
};

TypeScript/array-prototype-last.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
declare global {
5+
interface Array<T> {
6+
last(): T | -1;
7+
}
8+
}
9+
10+
Array.prototype.last = function() {
11+
return this[this.length - 1] ?? -1;
12+
};
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
type Fn = (accum: number, curr: number) => number
5+
6+
function reduce(nums: number[], fn: Fn, init: number): number {
7+
if (nums.length) {
8+
nums.forEach(num => init = fn(init, num));
9+
}
10+
return init;
11+
};

TypeScript/cache-with-time-limit.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
interface Value {
5+
value: number;
6+
tid: ReturnType<typeof setTimeout>;
7+
}
8+
9+
class TimeLimitedCache {
10+
private lookup: Map<number, Value>;
11+
12+
constructor () {
13+
this.lookup = new Map();
14+
}
15+
16+
set(key: number, value: number, duration: number): boolean {
17+
const found = this.lookup.has(key)
18+
if (found) {
19+
clearTimeout(this.lookup.get(key).tid)
20+
};
21+
this.lookup.set(key, {
22+
value: value,
23+
tid: setTimeout(() => this.lookup.delete(key), duration)
24+
});
25+
return found;
26+
}
27+
28+
get(key: number): number {
29+
return this.lookup.has(key) ? this.lookup.get(key).value : -1;
30+
}
31+
32+
count(): number {
33+
return this.lookup.size;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
function checkIfInstanceOf(obj: any, classFunction: any): boolean {
5+
if (obj === undefined) {
6+
return false;
7+
}
8+
while (obj !== null) {
9+
if (obj.constructor === classFunction) {
10+
return true;
11+
}
12+
obj = Object.getPrototypeOf(obj);
13+
}
14+
return false;
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// dfs
5+
function jsonStringify(object: any): string {
6+
let result = [];
7+
let dfs = (object: any) => {
8+
if (object === null) {
9+
result.push('null');
10+
return;
11+
}
12+
if (typeof object === 'number' || typeof object === 'boolean') {
13+
result.push(String(object));
14+
return;
15+
}
16+
if (typeof object === 'string') {
17+
result.push(`"${object}"`);
18+
return;
19+
}
20+
if (Array.isArray(object)) {
21+
result.push('[');
22+
for (let x of object) {
23+
dfs(x);
24+
result.push(',');
25+
}
26+
if (object.length) {
27+
result.pop();
28+
}
29+
result.push(']');
30+
return;
31+
}
32+
result.push('{');
33+
for (let key in object) {
34+
result.push(`"${key}":`)
35+
dfs(object[key]);
36+
result.push(',');
37+
}
38+
if (Object.keys(object).length) {
39+
result.pop();
40+
}
41+
result.push('}');
42+
}
43+
44+
dfs(object);
45+
return result.join('');
46+
};

TypeScript/counter.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
function createCounter(n: number): () => number {
5+
return () => n++;
6+
}

TypeScript/curry.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
function curry(fn: Function): Function {
5+
let result = [];
6+
console.log(fn.length)
7+
return function curried(...args) {
8+
result.push(...args);
9+
if (result.length < fn.length) {
10+
return curried;
11+
}
12+
return fn(...result);
13+
};
14+
};

TypeScript/debounce.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
type F = (...p: any[]) => any
5+
6+
function debounce(fn: F, t: number): F {
7+
let timeout = null
8+
return (...args) => {
9+
clearTimeout(timeout);
10+
timeout = setTimeout(fn, t, ...args);
11+
}
12+
};
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
function filter(arr: number[], fn: (n: number, i: number) => any): number[] {
5+
let result = [];
6+
arr.forEach((x, i) => {
7+
if (fn(x, i)) {
8+
result.push(x);
9+
}
10+
});
11+
return result;
12+
};
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(h)
3+
4+
type MultiDimensionalArray = (number | MultiDimensionalArray)[];
5+
6+
// dfs
7+
var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
8+
let result = [];
9+
let dfs = (arr: MultiDimensionalArray, n: number) => {
10+
for (let x of arr) {
11+
if (n === 0 || typeof x === 'number') {
12+
result.push(x);
13+
continue;
14+
}
15+
dfs(x, n - 1);
16+
}
17+
}
18+
19+
dfs(arr, n);
20+
return result;
21+
};

TypeScript/function-composition.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
type F = (x: number) => number;
5+
6+
function compose(functions: F[]): F {
7+
return (x: number) => functions.reduceRight((total: number, f: F) => f(total), x);
8+
};

TypeScript/group-by.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
declare global {
5+
interface Array<T> {
6+
groupBy(fn: (item: T) => string): Record<string, T[]>
7+
}
8+
}
9+
10+
// hash table
11+
Array.prototype.groupBy = function(fn) {
12+
const lookup = {};
13+
for (const val of this) {
14+
const key = fn(val);
15+
lookup[key] ||= []
16+
lookup[key].push(val);
17+
}
18+
return lookup;
19+
}

TypeScript/json-deep-equal.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(h)
3+
4+
// dfs
5+
function areDeeplyEqual(o1: any, o2: any): boolean {
6+
if (o1 === o2) {
7+
return true;
8+
}
9+
if (typeof o1 !== 'object' || o1 === null || typeof o2 !== 'object' || o2 === null) {
10+
return false;
11+
}
12+
if (Array.isArray(o1) !== Array.isArray(o2)) {
13+
return false;
14+
}
15+
const keys1 = Object.keys(o1);
16+
const keys2 = Object.keys(o2);
17+
if (keys1.length !== keys2.length) {
18+
return false;
19+
}
20+
return keys1.every(key => areDeeplyEqual(o1[key], o2[key]));
21+
};

TypeScript/memoize-ii.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(t)
3+
4+
type Fn = (...params: any) => any
5+
6+
// trie
7+
function memoize(fn: Fn): Fn {
8+
const trie = new Map();
9+
return function (...args) {
10+
let curr = trie;
11+
for (const arg of args) {
12+
if (!curr.has(arg)) {
13+
curr.set(arg, new Map());
14+
}
15+
curr = curr.get(arg);
16+
}
17+
if (!curr.has("_end")) {
18+
curr.set("_end", fn(...args));
19+
}
20+
return curr.get("_end");
21+
}
22+
}

TypeScript/memoize.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
type Fn = (...params: any) => any
5+
6+
function memoize(fn: Fn): Fn {
7+
const lookup = {};
8+
return (...args) => {
9+
if (lookup[args] !== undefined) {
10+
return lookup[args];
11+
}
12+
lookup[args] = fn(...args);
13+
return lookup[args];
14+
}
15+
}

TypeScript/promise-pool.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Time: O(n / c)
2+
// Space: O(c)
3+
4+
type F = () => Promise<any>;
5+
6+
function promisePool(functions: F[], n: number): Promise<any> {
7+
let nxt = () => functions[n++]?.().then(nxt);
8+
return Promise.all(functions.slice(0, n).map(fn => fn().then(nxt)));
9+
};

TypeScript/promise-time-limit.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
type Fn = (...params: any[]) => Promise<any>;
5+
6+
function timeLimit(fn: Fn, t: number): Fn {
7+
return async function(...args) {
8+
return Promise.race([
9+
fn.apply(this, args),
10+
new Promise((_, reject) => setTimeout(() => reject('Time Limit Exceeded'), t)),
11+
]);
12+
};
13+
};

TypeScript/sleep.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
async function sleep(millis: number): Promise<void> {
5+
await new Promise(resolve => setTimeout(resolve, millis));
6+
}

0 commit comments

Comments
 (0)