deno.land / x / masx200_leetcode_test@10.6.5 / sort-integers-by-the-power-value / index.ts
12345678910111213141516171819202122232425262728import { cache } from "./cache.ts";
export default getKth;function getKth(lo: number, hi: number, k: number): number { const arr = Array(hi - lo + 1) .fill(0) .map((_, i) => i + lo);
arr.sort((a, b) => { const w1 = getWeight(a); const w2 = getWeight(b);
return w1 === w2 ? a - b : w1 - w2; });
return arr[k - 1];}const getWeight = cache(function (x: number): number { if (x === 1) { return 0; } if (x % 2) { return getWeight(x * 3 + 1) + 1; } else { return getWeight(x / 2) + 1; }});
Version Info