Skip to content

Instantly share code, notes, and snippets.

@asen-ruuby
asen-ruuby / minCostClimbingStairs.js
Last active November 26, 2022 10:28
minCostClimbingStairs.js
// 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),
@asen-ruuby
asen-ruuby / stairs.js
Created November 25, 2022 16:58
stairs
function F(n) {
if (n < 0) {
return 0;
}
if (n === 0) {
return 1;
}
return F(n - 1) + F(n - 2);
}
@asen-ruuby
asen-ruuby / .js
Created November 6, 2022 18:21
JSON custom types serialization/parsing
const OBJECT_TYPE = '[object Object]';
const TYPE_PROP = '__type__';
const VALUE_PROP = '__value__';
const jsParseMap = {
'[object BigInt]': BigInt,
};
const jsSerializeMap = {
'[object BigInt]': (v) => `${v}`,
@asen-ruuby
asen-ruuby / .js
Created July 27, 2022 05:23
JSON.stringify
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');
@asen-ruuby
asen-ruuby / .js
Created July 25, 2022 16:22
Object entries fix
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]]);
}
@asen-ruuby
asen-ruuby / heap-sort.js
Last active July 11, 2022 14:01
heapSort
/**
* "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 {