Skip to content

Commit c1ad1a6

Browse files
update
1 parent 1768de7 commit c1ad1a6

12 files changed

+301
-0
lines changed

Eazy/118_Pascal'sTriangle.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} numRows
3+
* @return {number[][]}
4+
*/
5+
var generate = function(numRows) {
6+
let result = new Array(numRows).fill(null).map(_ => []);
7+
8+
for(let i = 0;i < numRows;i++) {
9+
result[i][0] = 1;
10+
result[i][i] = 1;
11+
}
12+
13+
for(let i = 1;i < numRows;i++) {
14+
for(let j = 1;j < i;j++) {
15+
result[i][j] = result[i-1][j-1] + result[i-1][j];
16+
}
17+
}
18+
19+
return result;
20+
};
21+
22+
export default generate;

Eazy/119_Pascal'sTriangleII.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number} rowIndex
3+
* @return {number[]}
4+
*/
5+
var getRow = function (rowIndex) {
6+
const result = new Array(rowIndex + 1).fill(null).map(_ => []);
7+
8+
for (let i = 0; i <= rowIndex; i++) {
9+
result[i][0] = 1;
10+
result[i][i] = 1;
11+
for (let j = 1; j < i; j++) {
12+
result[i][j] = result[i-1][j-1] + result[i-1][j];
13+
}
14+
}
15+
16+
return result[rowIndex]
17+
};
18+
19+
export default getRow;

Eazy/2637_PromiseTimeLimit.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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(async (resolve, reject) => {
9+
const timeOutId = setTimeout(() => {
10+
reject("Time Limit Exceeded")
11+
}, t);
12+
try {
13+
const rs = await fn(...args);
14+
resolve(rs)
15+
} catch (e) {
16+
reject(e)
17+
}
18+
clearTimeout(timeOutId)
19+
})
20+
}
21+
};
22+
23+
/**
24+
* const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100);
25+
* limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms
26+
*/
27+
28+
export default timeLimit;

Eazy/2677_ChunkArray.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {Array} arr
3+
* @param {number} size
4+
* @return {Array[]}
5+
*/
6+
var chunk = function(arr, size) {
7+
if(arr.length === 0) return [];
8+
const re = [];
9+
for(let i = 0;i < size;i++) {
10+
if(arr[i] !== undefined) {
11+
re.push(arr[i]);
12+
}else {
13+
break;
14+
}
15+
}
16+
arr.splice(0,size);
17+
18+
return [re, ...chunk(arr,size)];
19+
};
20+
21+
export default chunk;

Eazy/392_IsSubsequence.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
7+
// Dynamic programming
8+
var isSubsequence = function(s, t) {
9+
const sameChars = new Array(s.length+1).fill(null).map(_ => new Array(t.length+1).fill(0));
10+
11+
for(let i = 1;i <= s.length;i++) {
12+
for(let j = 1;j <= t.length;j++) {
13+
if(t[j-1] === s[i-1]) {
14+
sameChars[i][j] = sameChars[i-1][j-1] + 1;
15+
}else {
16+
sameChars[i][j] = Math.max(sameChars[i][j-1], sameChars[i-1][j]);
17+
}
18+
}
19+
}
20+
21+
return sameChars[s.length][t.length] === s.length ? true : false;
22+
};
23+
24+
export default isSubsequence;

Medium/2622_CacheWithTimeLimit.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var TimeLimitedCache = function () {
2+
this.cache = new Map();
3+
};
4+
5+
/**
6+
* @param {number} key
7+
* @param {number} value
8+
* @param {number} time until expiration in ms
9+
* @return {boolean} if un-expired key already existed
10+
*/
11+
TimeLimitedCache.prototype.set = function (key, value, duration) {
12+
const valueInCache = this.cache.get(key);
13+
14+
if (valueInCache) {
15+
clearTimeout(valueInCache.timeoutId)
16+
};
17+
18+
const timeoutId = setTimeout(() => {
19+
this.cache.delete(key);
20+
}, duration)
21+
22+
this.cache.set(key, { value, timeoutId });
23+
24+
return !!valueInCache;
25+
};
26+
27+
/**
28+
* @param {number} key
29+
* @return {number} value associated with key
30+
*/
31+
TimeLimitedCache.prototype.get = function (key) {
32+
return this.cache.get(key) ? this.cache.get(key).value : -1;
33+
};
34+
35+
/**
36+
* @return {number} count of non-expired keys
37+
*/
38+
TimeLimitedCache.prototype.count = function () {
39+
return this.cache.size;
40+
};
41+
42+
/**
43+
* Your TimeLimitedCache object will be instantiated and called as such:
44+
* var obj = new TimeLimitedCache()
45+
* obj.set(1, 42, 1000); // false
46+
* obj.get(1) // 42
47+
* obj.count() // 1
48+
*/
49+
50+
export default TimeLimitedCache;

Medium/2628_JSONDeepEqual.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {any} o1
3+
* @param {any} o2
4+
* @return {boolean}
5+
*/
6+
var areDeeplyEqual = function (o1, o2) {
7+
if (o1 === o2) return true;
8+
if (String(o1) !== String(o2)) return false;
9+
if (typeof o1 !== 'object') return o1 === o2;
10+
if (Object.keys(o1).length !== Object.keys(o2).length) return false;
11+
12+
for (let key of Object.keys(o1)) {
13+
if (!areDeeplyEqual(o1[key], o2[key])) {
14+
return false;
15+
}
16+
}
17+
18+
return true;
19+
};
20+
21+
export default areDeeplyEqual;

Medium/2632_Curry.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {Function} fn
3+
* @return {Function}
4+
*/
5+
var curry = function (fn) {
6+
return function curried(...args) {
7+
if (args.length >= fn.length) {
8+
return fn(...args);
9+
}
10+
11+
return curried.bind(this, ...args);
12+
};
13+
};
14+
15+
export default curry;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {any} object
3+
* @return {string}
4+
*/
5+
var jsonStringify = function(object) {
6+
if(typeof object === 'string' || object instanceof String) return `"${object}"`;
7+
8+
if(typeof object !== 'object' || object === null) return String(object);
9+
10+
if(Array.isArray(object)) {
11+
const arrToString = object.map(curr => jsonStringify(curr));
12+
return `[${arrToString.join()}]`;
13+
}
14+
15+
let objToString = Object.keys(object).map(key => `"${key}":${jsonStringify(object[key])}`);
16+
return `{${objToString.join()}}`
17+
};
18+
19+
export default jsonStringify;

Medium/2636_PromisePool.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {Function[]} functions
3+
* @param {number} n
4+
* @return {Function}
5+
*/
6+
var promisePool = async function (functions, n) {
7+
const evaluateNext = async () => {
8+
if (functions.length === 0) return;
9+
const currFn = functions.shift();
10+
await currFn();
11+
await evaluateNext();
12+
};
13+
const promiseArr = Array(n).fill().map(evaluateNext);
14+
await Promise.all(promiseArr);
15+
};
16+
17+
/**
18+
* const sleep = (t) => new Promise(res => setTimeout(res, t));
19+
* promisePool([() => sleep(500), () => sleep(400)], 1)
20+
* .then(console.log) // After 900ms
21+
*/
22+
23+
export default promisePool;

0 commit comments

Comments
 (0)