Skip to content

Commit a45c858

Browse files
committed
feat: Solved 4 Leetcode problems
* Solved 4 Leetcode problems from the 30 Days of JavaScript study plan
1 parent bddfc25 commit a45c858

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

.idea/.gitignore

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var TimeLimitedCache = function () {
2+
3+
};
4+
5+
/**
6+
* @param {number} key
7+
* @param {number} value
8+
* @param {number} duration time until expiration in ms
9+
* @return {boolean} if un-expired key already existed
10+
*/
11+
TimeLimitedCache.prototype.set = function (key, value, duration) {
12+
if (this.map === undefined) {
13+
this.map = new Map();
14+
this.accessible = new Map()
15+
}
16+
17+
const exists = this.map.has(key)
18+
this.map.set(key, value)
19+
this.accessible.set(key, true)
20+
21+
setTimeout(() => {
22+
if (this.map.get(key) === value)
23+
this.accessible.set(key, false)
24+
}, duration)
25+
26+
return exists
27+
};
28+
29+
/**
30+
* @param {number} key
31+
* @return {number} value associated with key
32+
*/
33+
TimeLimitedCache.prototype.get = function (key) {
34+
if (this.map === undefined)
35+
return -1
36+
37+
const accessible = this.accessible.get(key)
38+
39+
if (!accessible)
40+
return -1
41+
42+
return this.map.get(key)
43+
}
44+
45+
/**
46+
* @return {number} count of non-expired keys
47+
*/
48+
TimeLimitedCache.prototype.count = function () {
49+
let count = 0
50+
51+
if (this.map === undefined)
52+
return count
53+
54+
for (const accessible of this.accessible.values())
55+
if (accessible)
56+
count++
57+
58+
return count
59+
}
60+
61+
/**
62+
* const timeLimitedCache = new TimeLimitedCache()
63+
* timeLimitedCache.set(1, 42, 1000); // false
64+
* timeLimitedCache.get(1) // 42
65+
* timeLimitedCache.count() // 1
66+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {Function} fn
3+
* @param {number} t milliseconds
4+
* @return {Function}
5+
*/
6+
var debounce = function (fn, t) {
7+
let called = false
8+
let timer
9+
10+
return function (...args) {
11+
if (called)
12+
clearTimeout(timer)
13+
14+
timer = setTimeout(() => {
15+
fn(...args)
16+
called = false
17+
}, t)
18+
called = true
19+
}
20+
};
21+
22+
/**
23+
* const log = debounce(console.log, 100);
24+
* log('Hello'); // cancelled
25+
* log('Hello'); // cancelled
26+
* log('Hello'); // Logged at t=100ms
27+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {Array<Function>} functions
3+
* @return {Promise<any>}
4+
*/
5+
var promiseAll = function(functions) {
6+
return new Promise((resolve, reject)=>
7+
{
8+
(async ()=>{
9+
functions.forEach((func, i)=>{
10+
functions[i]=func().catch(err=>reject(err))
11+
})
12+
13+
for(let i=0;i<functions.length;i++)
14+
functions[i] = await functions[i];
15+
16+
resolve(functions)
17+
})()
18+
})
19+
}
20+
21+
/**
22+
* const promise = promiseAll([() => new Promise(res => res(42))])
23+
* promise.then(console.log); // [42]
24+
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {Array<Function>} functions
3+
* @return {Promise<any>}
4+
*/
5+
var promiseAll = function(functions) {
6+
return new Promise((resolve, reject)=>
7+
{
8+
let counter=0;
9+
10+
functions.forEach((func, i)=>{
11+
func()
12+
.then(result=>{
13+
functions[i]=result
14+
15+
if(++counter===functions.length)
16+
resolve(functions)
17+
})
18+
.catch(err=>reject(err))
19+
})
20+
})
21+
}
22+
23+
/**
24+
* const promise = promiseAll([() => new Promise(res => res(42))])
25+
* promise.then(console.log); // [42]
26+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {Function} fn
3+
* @param {number} t
4+
* @return {Function}
5+
*/
6+
var timeLimit = function(fn, t) {
7+
return async function(...args) {
8+
return new Promise((resolve, reject)=>{
9+
fn(...args).then(resolve).catch(reject)
10+
setTimeout(()=>reject("Time Limit Exceeded"),t)
11+
})
12+
}
13+
};
14+
15+
/**
16+
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
17+
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
18+
*/

0 commit comments

Comments
 (0)