Skip to content
RSS feed tkhwang on GitHub tkhwang on Twitter

Backtracking in javascript

✨ Algorithm

const backtrack = (cur, OTHER_ARGS...) => {
    if (BASE_CASE) {
        // do some logic
        return;
    }

    let res = 0;
    for (ITERATE_OVER_INPUT) {
        // modify the current input
        cur.push(CHANGE);

        ans += backtrack(cur, OTHER_ARGS...);

        // undo the modification of the current state
        cur.pop();
    }
    return res;
}

backtrack([], ...);

πŸ—’οΈ Typical problems

List

πŸ“š References