This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://leetcode.com/problems/min-cost-climbing-stairs/description/ | |
function minCostClimbingStairs(cost) { | |
const N = cost.length; | |
let [first, second] = cost; | |
for (let i = 2; i <= N; i++) { | |
[first, second] = [ | |
second, | |
(cost[i] ?? 0) + Math.min(second, first), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function F(n) { | |
if (n < 0) { | |
return 0; | |
} | |
if (n === 0) { | |
return 1; | |
} | |
return F(n - 1) + F(n - 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const OBJECT_TYPE = '[object Object]'; | |
const TYPE_PROP = '__type__'; | |
const VALUE_PROP = '__value__'; | |
const jsParseMap = { | |
'[object BigInt]': BigInt, | |
}; | |
const jsSerializeMap = { | |
'[object BigInt]': (v) => `${v}`, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = []; | |
for (let i = 0; i < 10_000_000; i++) { | |
arr.push(i); | |
} | |
console.time('JSON.stringify'); | |
JSON.stringify(arr); | |
console.timeEnd('JSON.stringify'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = Object.fromEntries([[Symbol('ok'), 123]]); | |
function getEntries(obj) { | |
const ownKeys = Reflect.ownKeys(obj); | |
const entries = []; | |
for (const key of ownKeys) { | |
if ({}.propertyIsEnumerable.call(obj, key)) { | |
entries.push([key, obj[key]]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* "If a is less than b, return 1, else if b is less than a, return -1, else return 0." | |
* | |
* The above function is a comparator function. It's used to compare two values. It's used by the | |
* sort() method to determine the order of the elements in the array | |
* @param {T} a - The first value to compare. | |
* @param {T} b - The second value to compare. | |
* @returns A function that takes two arguments and returns a number. | |
*/ | |
function maxHeap<T>(a: T, b: T): number { |