This file contains hidden or 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
/* | |
[[5, 3, 8, 1, 2, 4, 9, 7, 6, 5, 3, 0]] | |
[[3, 1, 2, 4, 3, 0], 5, [8, 9, 7, 6, 5]] | |
[[1, 2, 0], 3, [4, 3], 5, [7, 6, 5], 8, [9]] | |
[[0], 1, [2], 3, [3], 4, 5, [5, 6], 7, 8, 9] | |
[0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9] | |
*/ | |
function quickSort(array: number[]) { | |
const stack: (number[]|number)[][] = [[array]]; | |
let target: (number[]|number)[]; |
This file contains hidden or 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 mergeSort(array) { | |
const stack = [...array].map(n => [n]); | |
while (1) { | |
const nextStackItem = []; | |
const left = stack.shift(); | |
const right = stack.shift(); | |
let leftIndex = 0, rightIndex = 0; | |
if (left.length === array.length) { |
This file contains hidden or 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 asyncMap<T>(promiseFns: (() => Promise<T>)[], max: number) { | |
const result: T[] = []; | |
let count = 0; | |
let cursor = 0; | |
return new Promise(res => { | |
function run() { | |
while (count < max && cursor < promiseFns.length) { | |
count++; |
This file contains hidden or 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 PLAN_TYPE = Symbol('plan'); | |
const MOVE_TYPE = Symbol('move'); | |
const pillarMap = { | |
1: 2, // 0 + 1 | |
3: 0, // 1 + 2 | |
2: 1, // 2 + 0 | |
}; | |
function getWaypoint(start, end) { |
This file contains hidden or 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* numberOfCases(array, count) { | |
const stack = [[[], array]]; | |
let target; | |
while (target = stack.shift()) { | |
const [c, rest] = target; | |
if (c.length === count) yield c; | |
else stack.push(...rest.map((item, i) => [[...c, item], rest.filter((_, j) => j !== i)])); | |
} | |
} |
This file contains hidden or 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 ffi = require('ffi-napi'); | |
const user32 = new ffi.Library('user32', { | |
FindWindowA: ['ulong', ['string', 'string']], | |
FindWindowExA: ['ulong', ['ulong', 'ulong', 'string', 'ulong']], | |
GetDesktopWindow: ['ulong', []], | |
SetWindowLongPtrA: ['ulong', ['int', 'int', 'int']], | |
SetWindowPos: [ | |
'bool', | |
['ulong', 'ulong', 'int', 'int', 'int', 'int', 'uint'], |
This file contains hidden or 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* combination(array, n) { | |
if (n === 1) { | |
for (const item of array) yield [item]; | |
return; | |
} | |
for (let i = 0; i < array.length - n + 1; i++) { | |
const stack = [[[array[i]], array.slice(i + 1)]]; | |
while (stack.length) { | |
const [comb, rest] = stack.shift(); |
This file contains hidden or 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://stackoverflow.com/questions/836777/how-to-detect-browsers-protocol-handlers | |
// https://jsfiddle.net/stefansundin/0uca4h2o/ | |
function checkRegisteredProtocol(protocol: string) { | |
const input = document.createElement('input'); | |
input.style.position = 'absolute'; | |
input.style.opacity = '0'; | |
document.body.appendChild(input); | |
let isSupported = false; | |
input.focus(); | |
input.onblur = () => isSupported = true; |
This file contains hidden or 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* getDivisor(n) { | |
let temp = 0; | |
for (let i = 1; i <= n; i++) { | |
if (n % i) continue; | |
if (temp === i) break; | |
yield i; | |
temp = n / i; | |
yield temp; | |
} | |
} |
This file contains hidden or 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* numberOfCases(...args) { | |
const stack = [[[], args]]; | |
while (stack.length) { | |
const target = stack.shift(); | |
if (target) { | |
const [result, rest] = target; | |
for (let i = 0; i < rest.length; i++) { | |
let r; | |
yield r = [...result, rest[i]]; | |
stack.push([r, rest.filter((_, index) => index !== i)]); |