Skip to content

Commit 316e453

Browse files
committed
update
1 parent 95fa562 commit 316e453

21 files changed

+366
-1
lines changed

README.md

Lines changed: 35 additions & 1 deletion
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
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
Lines changed: 10 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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+
};
Lines changed: 11 additions & 0 deletions
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+
};
Lines changed: 35 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
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+
};
Lines changed: 46 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)